提交时间:2023-06-30 12:28:48
运行 ID: 21474
#include<bits/stdc++.h> using namespace std; string s1, s2; int a[2010], b[2010], c[2010]; int c_to_d(char x) { if(x >= '0' && x <= '9') return x - '0'; if(x >= 'A') return x - 'A' + 10; } char d_to_c(int d) { if(d <= 9) return d + '0'; return d - 10 + 'A'; } int main() { int B; cin >> B; cin >> s1 >> s2; int len1 = s1.size(), len2 = s2.size(); for (int i = 0; i < len1; i++) a[len1 - 1 - i] = c_to_d(s1[i]); for (int i = 0; i < len2; i++) b[len2 - 1 - i] = c_to_d(s2[i]); int mlen = max(len1, len2); int jin = 0; for (int i = 0; i < mlen; i++) { c[i] = a[i] + b[i] + jin; jin = c[i] / B; c[i] = c[i] % B; } if(jin != 0) { mlen++; c[mlen - 1] = jin; } while(c[mlen - 1] == 0 && mlen - 1 != 0) mlen--; for (int i = mlen - 1; i >= 0; i--) cout << d_to_c(c[i]); return 0; }