考察编程基础知识,用到字符和数字相互转化等。形式是描述清楚明文和暗文的转化规则。
1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXLEN 71 5 6 int length; 7 8 void toCode(int code[],char text[]){ 9 int i; 10 for(i=0;i<length;i++){ 11 if(text[i] == ‘_‘) 12 code[i] = 0; 13 else if(text[i] == ‘.‘) 14 code[i] = 27; 15 else 16 code[i] = text[i] - 96; 17 } 18 } 19 20 void toText(char text[],int code[]){ 21 int i; 22 for(i=0;i<length;i++){ 23 if(code[i] == 0) 24 text[i] = ‘_‘; 25 else if(code[i] == 27) 26 text[i] = ‘.‘; 27 else 28 text[i] = code[i] + 96; 29 } 30 text[i] = 0; 31 } 32 33 int inRange(int n){ 34 if(n>=0&&n<=27) 35 return 1; 36 return 0; 37 } 38 39 void untwist(char textC[],int k){ 40 int i,j; 41 int codeC[MAXLEN],codeP[MAXLEN]; 42 char textP[MAXLEN]; 43 44 toCode(codeC,textC); 45 for(i=0;i<length;i++){ 46 j = (k*i)%length; 47 if(inRange(codeC[i] + i)) 48 codeP[j] = codeC[i] + i; 49 else if(inRange(codeC[i] + i - 28)) 50 codeP[j] = codeC[i] + i - 28; 51 else if(inRange(codeC[i] + i - 56)) 52 codeP[j] = codeC[i] + i - 56; 53 else if(inRange(codeC[i] + i - 84)) 54 codeP[j] = codeC[i] + i - 84; 55 } 56 toText(textP,codeP); 57 printf("%s\n",textP); 58 } 59 60 int main(){ 61 int k; 62 char textC[MAXLEN]; 63 while(scanf("%d",&k)&&k){ 64 scanf("%s",textC); 65 length = strlen(textC); 66 untwist(textC,k); 67 } 68 return 0; 69 }
时间: 2024-10-07 10:51:40