ZOJ Problem Set

今天在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]!='')
        {
            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]='';

        printf("%s
",ptext);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/xlturing/p/3300515.html