题目:1048 数字加密 (20 分)
来源:PAT (Basic Level) Practice
题面
题意:给定两个数字A,B,按给定规律对奇偶位分别操作。
思路:建议字符串输入,如果一个字符串长度小于另一个的话,前面要用零补上。
Code
点击查看代码
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
string a, b;
int main() {
bool f = false;
cin >> a >> b;
int ns = max(a.size(),b.size()) - min(a.size(), b.size());
if (a.size() > b.size()) {
for (int i = 0; i < ns; i++)
b = '0' + b;
}
else if (a.size() < b.size()) {
for (int i = 0; i < ns; i++)
a = '0' + a;
}
if (a.size() % 2 == 0)f = true;
for (int i = 0; i < b.size(); i++) {
if (!f) {
f = true;
int t = a[i] - '0' + b[i] - '0';
t = t % 13;
if (t == 10)cout << "J";
else if (t == 11)cout << "Q";
else if (t == 12)cout << "K";
else cout << t;
}
else if (f) {
f = false;
int t = b[i] - '0' - (a[i] - '0');
if (t >= 0)cout << t;
else cout << t + 10;
}
}
cout << "\n";
return 0;
}