提交时间:2024-08-19 21:14:42

运行 ID: 167954

#include <bits/stdc++.h> #include <cstring> using namespace std; string d = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int main() { int k; string a, b; cin >> k >> a >> b; int p = 1; reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); int ansa = 0, ansb = 0; for (int i = 0; i < a.size(); i++) { if (a[i] >= '0' && a[i] <= '9') ansa += p * (a[i] - 48); else ansa += p * (a[i] - 55); p *= k; } p = 1; for (int i = 0; i < b.size(); i++) { if (b[i] >= '0' && b[i] <= '9') ansb += p * (b[i] - 48); else ansb += p * (b[i] - 55); p *= k; } int ans = ansa + ansb; string sum = ""; while (ans != 0) { sum += d[ans % k]; ans /= k; } for (int i = sum.length() - 1; i >= 0; i--) cout << sum[i]; cout << endl; return 0; }