Submission
Status:
PPPPPPPPP
Subtask/Task Score:
100/100
Score: 100
User: ST68031
Problemset: บวกเลขฐาน
Language: cpp
Time: 0.002 second
Submitted On: 2025-10-11 11:06:43
#include <bits/stdc++.h>
using namespace std;
int charToVal(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'Z') return c - 'A' + 10;
if (c >= 'a' && c <= 'z') return c - 'a' + 10;
return 0;
}
char valToChar(int v) {
if (v < 10) return '0' + v;
return 'A' + (v - 10);
}
int main() {
int base;
string base1, base2;
cin >> base >> base1 >> base2;
int a = base1.size(), b = base2.size();
int i = a - 1, j = b - 1, carry = 0;
string ans = "";
while (i >= 0 || j >= 0 || carry > 0) {
int temp1 = 0, temp2 = 0;
if (i >= 0) temp1 = charToVal(base1[i--]);
if (j >= 0) temp2 = charToVal(base2[j--]);
int sum = temp1 + temp2 + carry;
carry = sum / base;
sum %= base;
ans.push_back(valToChar(sum));
}
reverse(ans.begin(), ans.end());
cout << ans << endl;
return 0;
}