PAT (Basic Level) Practice 1048 数字加密 (20分) 急需大神解惑!

1.题目

本题要求实现一种数字加密方法。首先固定一个加密用正整数 A,对任一正整数 B,将其每 1 位数字与 A 的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对 13 取余——这里用 J 代表 10、Q 代表 11、K 代表 12;对偶数位,用 B 的数字减去 A 的数字,若结果为负数,则再加 10。这里令个位为第 1 位。

输入格式:

输入在一行中依次给出 A 和 B,均为不超过 100 位的正整数,其间以空格分隔。

输出格式:

在一行中输出加密后的结果。

输入样例:

1234567 368782971

输出样例:

3695Q8118

2.问题说明

使用注释中的三目运算符就会测试点1 4 5错,写开就全对……不知道三目运算符哪里写错了??

(另外我是真没从题目中看出来要补零……)

3.代码

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
	string a, b;
	char t;
	cin >> a >> b;
	reverse(a.begin(), a.end());
	reverse(b.begin(), b.end());
	int aa = a.length();
	int bb = b.length();
	if (aa>bb)
	{
		for (int i = 0; i < (aa-bb); i++)
			b.push_back('0');
	}
	else
	{
		for (int i = 0; i < (bb-aa); i++)
			a.push_back('0');
	}
	for (int i = 0; i <a.length(); i++)
	{
		int  temp = 0;
		if ((i+1) % 2 != 0)
		{
			 temp = ((a[i] - 48) + (b[i] - 48)) % 13;
			if (temp >= 10) { if (temp == 10)t = 'J'; else if (temp == 11)t = 'Q'; else t = 'K'; }
			else t = temp + 48;
		}
		else
		{
			temp= (b[i] - 48) - (a[i] - 48);
			if (temp < 0)temp = temp + 10;
			//temp = ((b[i] - 48) - (a[i] - 48))>0 ? ((b[i] - 48) - (a[i] - 48)) : ((b[i] - 48) - (a[i] - 48)) + 10;
			t = temp +48;
		}
		b[i] = t;
	}
	reverse(b.begin(), b.end());

	cout << b;

}
原文地址:https://www.cnblogs.com/Jason66661010/p/12788943.html