【PAT】1048数字加密
程序员文章站
2022-07-15 13:39:35
...
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
string a,b,tb;
int min;
cin>>a>>b;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int alen = a.length(),blen = b.length();
if(blen < alen){//b没有a长则补齐
for(int i = 0; i < alen - blen; i++)
b += '0';
}
min = alen;
for(int i = 0; i < min; i++){
int j = i + 1;
if(j % 2 != 0){//奇数
int t = (a[i] - '0' + b[i] - '0') % 13;
if(t == 10) b[i] = 'J';
else if(t == 11) b[i] = 'Q';
else if(t == 12) b[i] = 'K';
else b[i] = (char)(t + '0');
}else{//偶数
int t = (int)(b[i] - a[i]);
if(t >= 0){
b[i] = (char)(t + '0');
}else{
b[i] = (char)(t + '0' + 10);
}
}
}
reverse(b.begin(),b.end());
cout<<b<<endl;
return 0;
}
有一个坑就是,如果b比a短,需要补齐,在网上搜了好多方法都是一大堆从后往前的,我懒,直接reverse了,blen,alen是必须的,不然在添加0的时候会少添加一倍,因为b.length()在循环里会变大,所以blen,alen不能替换成b.length(),a.length() 上一篇: PAT 1048 数字加密