提交时间:2024-08-19 22:58:21

运行 ID: 168137

#include<bits/stdc++.h> using namespace std; string addInNBase(int N, const string& num1, const string& num2) { string result; int carry = 0; int i = num1.length() - 1; int j = num2.length() - 1; while (i >= 0 || j >= 0 || carry > 0) { int digit1 = (i >= 0)? num1[i] - '0' : 0; int digit2 = (j >= 0)? num2[j] - '0' : 0; int sum = digit1 + digit2 + carry; carry = sum / N; sum %= N; result.push_back(sum + '0'); i--; j--; } reverse(result.begin(), result.end()); return result; } int main() { int N; string num1, num2; cin >> N >> num1 >> num2; cout << addInNBase(N, num1, num2) << endl; return 0; }