ZOJ Problem Set - 1006 Do the Untwist

时间:2023-03-09 02:35:03
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[];
char ptext[],ctext[];
while(scanf("%d",&k)!=EOF&&k)
{
scanf("%s",ctext); int n=;
while(ctext[n]!='\0')
{
if(ctext[n]=='.')
{
ccode[n]=;
}
else if(ctext[n]=='_')
{
ccode[n]=;
}
else
{
ccode[n]=ctext[n]-'a'+;
}
n++;
} //key
for(int i=;i<n;i++)
{
int tem;
tem=(ccode[i]+i)%;
if(tem==)
ptext[k*i%n]='_';
else if(tem==)
ptext[k*i%n]='.';
else
ptext[k*i%n]=tem+'a'-; }
ptext[n]='\0'; printf("%s\n",ptext);
} return ;
}