题目

易错点:如果字符串a>b,要把b前面补0,并且这些0要参与运算。但是题目中并没有说明啊???
PAT1048 数字加密 (20 分)

代码

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string strA;
	string strB;

	cin >> strA >> strB;

	int lenA = strA.length();
	int lenB = strB.length();
	//放进数组中
	int a[100] = { 0 };
	int b[100] = { 0 };
	int aSize = 0;
	int bSize = 0;
	int i;

	for (i = lenA - 1; i >= 0; i--)
	{
		a[aSize] = strA[i] - '0';
		aSize++;
	}

	for (i = lenB - 1; i >= 0; i--)
	{
		b[bSize] = strB[i] - '0';
		bSize++;
	}
	//转换 
	int j;
	for (i = 0; i < lenA; i += 2)//奇数 
	{
		b[i] = b[i] + a[i];
		b[i] %= 13;
	}

	for (j = 1; j < lenA; j += 2)//偶数 
	{
		b[j] -= a[j];
		if (b[j] < 0)
		{
			b[j] += 10;
		}
	}

	//输出
	for (j = (lenA > lenB ? lenA : lenB) - 1; j >= 0; j--)
	{
		if (b[j] == 10)cout << 'J';
		else if (b[j] == 11)cout << 'Q';
		else if (b[j] == 12)cout << 'K';
		else cout << b[j];
	}
	cout << endl;
	return 0;
}

相关文章: