PAT 1048. 数字加密(20)

来源:互联网 发布:ros 禁止mac上网 编辑:程序博客网 时间:2024/04/29 09:34

本题要求实现一种数字加密方法。首先固定一个加密用正整数A,对任一正整数B,将其每1位数字与A的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对13取余——这里用J代表10、Q代表11、K代表12;对偶数位,用B的数字减去A的数字,若结果为负数,则再加10。这里令个位为第1位。

输入格式:

输入在一行中依次给出A和B,均为不超过100位的正整数,其间以空格分隔。

输出格式:

在一行中输出加密后的结果。

输入样例:
1234567 368782971
输出样例:

3695Q8118

坑:大家都知道A位数小于B时,A不足为用0补足,,B位数小于A时,也要在B前用0补足

#include <stdio.h>  #include <string.h>    int main()  {      int i,j,k,len,lena,lenb,temp,tempa,tempb,flag=0;      char a[100]= {NULL};      char b[100]= {NULL};      char c[100]= {NULL};      scanf("%s %s",a,b);      lena = strlen(a);      lenb = strlen(b);       len = (lena>lenb)?lena:lenb;      j = lena-1;      i = lenb-1;      for (k=len-1; k>=0; k--)      {          flag++;          tempb = (i>=0)?b[i] -'0':0;          tempa = (j>=0)?a[j] -'0':0;          if (flag%2)          {              temp = (tempa + tempb)%13;              if (temp<10)                  c[k] = temp +'0';              if (temp==10)                  c[k] = 'J';              if (temp==11)                  c[k] = 'Q';              if (temp==12)                  c[k] = 'K';          }          else          {              temp = tempb - tempa;              if (temp<0)                  temp=temp+10;              c[k] = temp +'0';          }         j--;         i--;      }      puts(c);      return 0;  }  


0 0
原创粉丝点击