您的位置:首页 > 其它

PAT.1048 数字加密

2017-05-08 18:10 155 查看
1.当B的长度大于A时,直接将B前面的复制过去

2.当A的长度大于B时,B前面要补0,并且要判断奇数位偶数位

3.当A为10000,B为1时,要判断为0时应该打印0,最后打印10001,之前没判断直接A的那位数变负数再加10,所以会出问题

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
char a[105],b[105],c[105];
cin >> a >> b;
int i = strlen(a)-1;
int j = strlen(b)-1;
//cout << i << j<<endl;
int count=0;

while(i>=0&&j>=0)
{
if(count % 2 == 0)
{

int x = (a[i]-'0' + b[j]-'0')%13;
// cout << " x " <<x << endl;
switch(x)
{
case 10:c[count++] = 'J';
break;
case 11:c[count++] = 'Q';
break;
case 12:c[count++] = 'K';
break;
default:c[count++] = x+'0';
break;
}
i--;
j--;
// cout << "count "<<count<<endl;
}
else
{

int y;
y = (b[j]-'0') - (a[i] - '0');

if(y<0)
y += 10;
// cout << " " << y << endl;
c[count++] = y + '0';
i--;
j--;
}

}

while(j>=0)
{
c[count++] = b[j];
j--;
}

while(i>=0)
{
if(count % 2 == 0)
{
c[count++] = a[i];

}
else
{
if(a[i]=='0')
c[count++] = a[i];
else
c[count++] = -(a[i]-'0')+10+'0';
}
i--;

}

for(i=strlen(c)-1;i>=0;i--)
cout << c[i];
cout << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: