密码破译.docVIP

  • 1
  • 0
  • 约 9页
  • 2016-08-05 发布于河南
  • 举报
密码破译

1.链表——移动小球 //这道题解强调小球之间的相对顺序,而非绝对顺序; #include iostream using namespace std; struct node { int left; int right; }; //我们定义了结构体node,并用left,right来表示小球i的左边和右边小球的编号。 int main() { int t,n,m,i,oper,b1,b2; //针对小球b1和b2进行oper操作 scanf(%d,t); while(t--) { cinnm; struct node *ball = new node[n+6]; //动态分配内存 ball[0].right = 1; for(i = 1;i=n;i++) ball[i].left = i - 1,ball[i].right = i + 1; for(i = 0;im;i++) { //移动的过程分为两步:把小球X移出序列;把X重新插入序列 cinoperb1b2; if(oper == 1) { ball[ball[b1].left].right = ball[b1].right; //将b1移出序列 ball[ball[b1].right].left = ball[b1].left; ball[ball[b2].left].right = b1; //将b1重新插入序列 ball[b1].left = ball[b2].left; ball[b1].right = b2; //这两行不能与上两行颠倒(因为相对位置动态改变中) ball[b2].left = b1; } else if(oper == 2) { ball[ball[b1].left].right = ball[b1].right; //将b1移出序列 ball[ball[b1].right].left = ball[b1].left; ball[b1].right = ball[b2].right; //将b1重新插入序列 ball[ball[b2].right].left = b1; ball[b2].right = b1; //这两行不能与上两行颠倒 ball[b1].left = b2; } } int x = 0; for(i = 0;in;i++) { x = ball[x].right; coutx ; } } return 0; } ---2.密码破译 ----------------注意输出结果题意的理解(其实编号) 若找到两把破译的钥匙,则输出文件有两行,分别为两把钥匙的编号,按从小到大输出。若有多种破译方案,则只输出包含起始编号最小的一组即可。 #includestdio.h int main(){ int N,L,a[1000],count=0,m=0,c[100],b[100],min,e,f; scanf(%d%d,N,L); for(int i=1;i=N;i++) scanf(%d,a+i); for( i=1;i=N;i++) for(int j=i+1;j=N;j++) if(a[i]+a[j]==L) {count++;b[m]=i;c[m]=j;m++;} if(

文档评论(0)

1亿VIP精品文档

相关文档