1048. 数字加密(20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
本题要求实现一种数字加密方法。首先固定一个加密用正整数A,对任一正整数B,将其每1位数字与A的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对13取余——这里用J代表10、Q代表11、K代表12;对偶数位,用B的数字减去A的数字,若结果为负数,则再加10。这里令个位为第1位。
输入格式:
输入在一行中依次给出A和B,均为不超过100位的正整数,其间以空格分隔。
输出格式:
在一行中输出加密后的结果。
输入样例:1234567 368782971输出样例:
3695Q8118
这道题的测试用例有点坑。 当B字符串小于A字符串的长度时,B字符串要往后移,前面要补零,直到B字符串新的长度和A的长度相等的时候再停止
代码如下;(写的不好)
#include <bits/stdc++.h> using namespace std; char num[13] = {'0','1','2','3','4','5','6','7','8','9','J','Q','K'}; int main() { string A,B; cin>>A>>B; int lena = A.length(); int lenb = B.length(); int cnt = 1; int ss[2000]; memset(ss,0,sizeof(ss)); for(int i=0; i<2000; i++) ss[i] = 0; if(lenb > lena ) ////当B的长度大于A的长度的时候 { for(int i=0; i<lenb-lena; i++) cout<<B[i]; } int p= lena - lenb; while(p>0) ///B字符串后移 { p--; B = "0"+B; } lenb = B.length(); for(int i=lena-1, j = lenb -1; i>=0 && j>=0; i--,j--) { if(cnt%2 != 0) { int temp = A[i] -'0' + B[j]-'0'; temp = temp%13; ss[cnt] = temp; cnt++; continue; } if(cnt%2 == 0) { int temp = B[j] - A[i]; if(temp<0) temp+=10; ss[cnt] = temp; // cout<<ss[cnt]<<endl; cnt++; } } for(int i=cnt-1; i>0; i--) cout<<num[ss[i]]; return 0; }