提交时间:2024-08-19 21:54:02
运行 ID: 168027
#include <iostream> #include <string> #include <algorithm> using namespace std; int baseNToDecimal(const string& num, int base) { int decimalValue = 0; int power = 1; for (int i = num.size() - 1; i >= 0; --i) { char digit = num[i]; int digitValue; if (digit >= '0' && digit <= '9') { digitValue = digit - '0'; } else { digitValue = digit - 'A' + 10; } decimalValue += digitValue * power; power *= base; } return decimalValue; } string decimalToBaseN(int decimal, int base) { if (decimal == 0) return "0"; string baseNStr; while (decimal > 0) { int remainder = decimal % base; if (remainder < 10) { baseNStr.push_back('0' + remainder); } else { baseNStr.push_back('A' + (remainder - 10)); } decimal /= base; } reverse(baseNStr.begin(), baseNStr.end()); return baseNStr; } int main() { int N; string num1, num2; cin >> N >> num1 >> num2; int decNum1 = baseNToDecimal(num1, N); int decNum2 = baseNToDecimal(num2, N); int sum = decNum1 + decNum2; string result = decimalToBaseN(sum, N); cout << result << endl; return 0; }