PTA B1048 数字加密 (20 分)
作者:互联网
题目链接:点击这里
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
const int MOD = 10000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 1010;
int a[maxn];
int main()
{
string a,b,ans;
cin>>a>>b;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int lena = a.length();
int lenb = b.length();
int len = lena>lenb?lena:lenb; //a和b的较大长度
for(int i=0;i<len;i++)
{
int numa = i<lena?a[i]-'0':0; //numa对应a[i],长度不足时用0代替
int numb = i<lenb?b[i]-'0':0; //numb对应b[i],长度不足时用0代替
if(i%2==0) //字符串的偶数位,即原数的奇数位
{
int temp = (numa+numb)%13;
if(temp==10) ans += 'J';
else if(temp==11) ans += 'Q';
else if(temp==12) ans += 'K';
else ans += temp+'0';
}
else //字符串的奇数位,即原数的偶数位
{
int temp = numb - numa;
if(temp<0) ans+=temp+10+'0';
else ans+=temp+'0';
}
}
reverse(ans.begin(), ans.end());
cout<<ans<<endl;
return 0;
}
标签:lenb,lena,20,int,PTA,long,const,include,B1048 来源: https://blog.csdn.net/qq_42815188/article/details/100921989