Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我的代码,一直RE,从Java到C都是,实在疯了,那位大牛能帮我看看么,太谢谢了// 1026 Cipher #include <stdio.h> #include <string.h> void encryptKtimes(int n,int k); void getCyc(int n); /* void printKey(int n); void printCyc(int n); */ int key[2000]; int cyc[2000]; char message[2000]; int cycnum = 0; int main() { int n,k,len; // 0 < n <= 200 while (1) { scanf("%d",&n); if (0==n) break; for (int i=0;i<n;i++) { scanf("%d",&key[i]); key[i]--; } getCyc(n); //printCyc(n); while(1) { scanf("%d",&k); if (0==k) break; getchar(); // 掠过那个空格 // 假设message长度为len(len<=n),在0-(len-1)被填充为字符,len填充为0. gets(message); len = strlen(message); // 把len-(n-1)这(n-len)这字符设置为空格. if (len<=n) memset(message+len,' ',n-len); message[n]=0; // 最后nth位置设为0以便输出. //printf("c:%c length:%d\n",message[0],strlen(message)); encryptKtimes(n,k); // 编码k次 printf("%s\n",message); } } return 0; } void encryptKtimes(int n,int k) { int i,j,jj,times; char ch1,ch2; for(i=1;i<=cycnum;i++) { // 求此轮换长度和第一个数的位置 int length = 0; int pos = 0; for(j=n-1;j>=0;j--) { if (cyc[j]==i) { length++; pos = j; } } times = k % length; // 实际需要做多少次 //printf("k:%d\n",k); for (j=0;j<times;j++) { ch1 = message[pos]; ch2=message[key[pos]]; for (jj=0;jj<length;jj++) { //printf("ch1:%c ch2:%c\n",ch1,ch2); message[key[pos]] = ch1; pos = key[pos]; ch1 = ch2; ch2 = message[key[pos]]; //printf("message:%s\n",message); } } } } // key = {4 5 3 7 2 8 1 6 10 9}时 // cyc = 1231241455 void getCyc(int n) { memset(cyc,0,n); // 初始化 int count = 0; // 处理了多少个数 int num = 0; // 一共有多少个轮换 int pos = 0; do { pos = 0; while (cyc[pos]!=0)pos++; num++; while (cyc[pos]==0) { count++; cyc[pos] = num; pos = key[pos]; } } while(count<n); cycnum = num; // 上面这个例子的话会返回5. } /* // for test void printKey(int n) { for (int i=0;i<n;i++) { printf("%d ",key[i]); } printf("\n"); } void printCyc(int n) { for (int i=0;i<n;i++) { printf("%d ",cyc[i]); } printf("\n"); } */ Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator