提交时间:2024-08-21 17:11:22

运行 ID: 180659

#include <bits/stdc++.h> using namespace std; long long convertToDecimal(const string& number, int base) { long long result = 0; for (int i = 0; i < number.size(); i++) { char digit = number[number.size() - 1 - i]; if (isdigit(digit)) { result += (digit - '0') * pow(base, i); } else { result += (toupper(digit) - 'A' + 10) * pow(base, i); } } return result; } string convertToBase(long long decimal, int base) { if (decimal == 0) return "0"; string result; while (decimal > 0) { int remainder = decimal % base; if (remainder < 10) { result += (remainder + '0'); } else { result += (remainder - 10 + 'A'); } decimal /= base; } reverse(result.begin(), result.end()); return result; } int main() { int N; string num1, num2; cin >> N >> num1 >> num2; long long dec1 = convertToDecimal(num1, N); long long dec2 = convertToDecimal(num2, N); long long sum = dec1 + dec2; string result = convertToBase(sum, N); cout << result; return 0; }