测试点1,3:是因为最后得到的PAT数可能会超出N位, 如:
30527
26203
20415 // 输出为 117201测试点5:是因为可能得到的PAT数为0,此时需输出0
正确代码
#include<iostream>
using namespace std;
int main() {
string table, num1, num2, res;
int temp = 0, c = 0, len;
cin >> table >> num1 >> num2;
len = table.length();
if(num1.length() < len) num1.insert(0, len - num1.length(), '0');
if(num2.length() < len) num2.insert(0, len - num2.length(), '0');
for(int i = len - 1; i >= 0; i--) {
int temp = table[i] -'0' == 0 ? 10 : table[i] -'0', sum = 0;
if(num1[i] != '0' || num2[i] != '0'){
res = res + to_string((num1[i] - '0' + num2[i] - '0' + c) % temp);
} else res = res + to_string(c % temp);
c = (num1[i] - '0' + num2[i] - '0' + c) / temp;
}
if(c) res = res + to_string(c);
temp = 0;
for(int i = res.length() - 1; i >= 0; i--) {
if(res[i] != '0') temp = 1;
if(temp) cout << res[i];
}
if(temp == 0) cout << 0;
return 0;
}