您的位置:首页 > 其它

PAT 乙级 1048 数字加密 (20 分)

2019-08-15 22:35 113 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/m0_38088647/article/details/99659691

题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805276438282240

经验总结:
注意A和B的中较短的字符串需要在其之前补0,补到两字符串长度相等。可以真的补0,也可以假装补0,例如如下代码为假装补0.
记:reverse_iterator 中的rbegin 到rend 是++而不是–。

C++代码:

#include <iostream>
#include <stack>
using namespace std;
char c[13]={'0','1','2','3','4','5','6','7','8','9','J','Q','K'};
int main(){
string a,b;
cin>>a>>b;
stack<char> result;
int i = 1;
string::reverse_iterator it1 = a.rbegin(),it2 = b.rbegin();
for(;it1!=a.rend()&&it2!=b.rend();it1++,it2++,i++){
int bnum = *it2-'0';
int anum = *it1-'0';
if(i%2==0){
result.push(c[(bnum-anum+10)%10]);
}else{
result.push(c[(anum+bnum)%13]);
}
}
while(it1!=a.rend()){
if(i%2==0){
result.push(c[('0'-*it1+10)%10]);
}else{
result.push(*it1);
}
i++;
it1++;
}
while(it2!=b.rend()){
result.push(*it2);
it2++;
}
while(!result.empty()){
cout<<result.top();
result.pop();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: