ZOJ Problem Set - 1006 Do the Untwist
今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算:
比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = A+C + D*n 当B<D时,两边对D取摸, B = B%D = ( A+C + D*n )%D = (A+C)%D
由此可得此题答案,见代码
#include <cstdio> #include <cstring> int main() { int k,ccode[80]; char ptext[80],ctext[80]; while(scanf("%d",&k)!=EOF&&k) { scanf("%s",ctext); int n=0; while(ctext[n]!='\0') { if(ctext[n]=='.') { ccode[n]=27; } else if(ctext[n]=='_') { ccode[n]=0; } else { ccode[n]=ctext[n]-'a'+1; } n++; } //key for(int i=0;i<n;i++) { int tem; tem=(ccode[i]+i)%28; if(tem==0) ptext[k*i%n]='_'; else if(tem==27) ptext[k*i%n]='.'; else ptext[k*i%n]=tem+'a'-1; } ptext[n]='\0'; printf("%s\n",ptext); } return 0; }