其他分享
首页 > 其他分享> > PAT 乙级 1048 数字加密 (20分)

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

作者:互联网

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
	string a, b;
	cin >> a >> b;
	string result;
	while (a.length() != b.length()) 
	{
		if (a.length() > b.length())
			b = '0' + b;
		else
			a = '0'+a;
	}
	int res = 0, res1 = 0;
	int count = 0;
	for (int i = a.length()-1; i >=0; i--)
	{
		count=count+1;
		switch (count%2)
		{
		case 0:
			 res = (b[i]-'0')-(a[i]-'0');
			if (res < 0)
				result = result + to_string(res + 10);
			else
				result = result + to_string(res);
			break;
		case 1:
			 res1 = ((b[i] - '0') + (a[i] - '0')) % 13;
			if (res1 >= 0 && res1 < 10)
				result = result + to_string(res1);
			if(res1==10)
				result = result + 'J';
			if (res1 == 11)
				result = result + 'Q';
			if (res1 == 12)
				result = result + 'K';
			break;
		default:
			break;
		}
	}
	reverse(result.begin(),result.end());
	cout << result << endl;
	system("pause");
	return 0;
}

标签:1048,PAT,string,int,res,res1,length,result,20
来源: https://blog.csdn.net/qq_43180487/article/details/110243962