提交时间:2024-08-19 18:17:38

运行 ID: 167822

#include <iostream> #include <string> #include <algorithm> std::string f(int base, std::string num1, std::string num2) { std::reverse(num1.begin(), num1.end()); std::reverse(num2.begin(), num2.end()); int carry = 0; std::string r; int i = 0, j = 0; while (i < num1.size() || j < num2.size() || carry) { int digit1 = (i < num1.size())? (num1[i] >= '0' && num1[i] <= '9')? num1[i] - '0' : num1[i] - 'A' + 10 : 0; int digit2 = (j < num2.size())? (num2[j] >= '0' && num2[j] <= '9')? num2[j] - '0' : num2[j] - 'A' + 10 : 0; int sum = digit1 + digit2 + carry; carry = sum / base; sum %= base; char digitChar = sum < 10? sum + '0' : sum - 10 + 'A'; r.push_back(digitChar); i++; j++; } std::reverse(r.begin(), r.end()); return r; } int main() { int base; std::string num1, num2; std::cin >> base >> num1 >> num2; std::string sum = f(base, num1, num2); std::cout << sum << std::endl; return 0; }