Submission

Status:

PPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: Bestzu

Problemset: บวกเลขฐาน

Language: cpp

Time: 0.003 second

Submitted On: 2025-10-13 22:43:01

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

int toDeci(string x, int base) {
    int ans = 0;
    for(int i = 0; i < x.size(); i++) {
        ans *= base;
        if(x[i] >= '0' && x[i] <= '9') ans += x[i] - '0';
        else ans += x[i] - 'A' + 10;
    }
    return ans;
}

string toBase(int sum, int base) {
    if(sum == 0) return "0";
    string ans = "";
    while(sum > 0) {
        int r = sum % base;
        if(r <= 9) ans += char(r + '0');
        else ans += char(r - 10 + 'A');
        sum /= base;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);

    int base; cin >> base;
    string a, b; cin >> a >> b;

    int x = toDeci(a, base);
    int y = toDeci(b, base);
    
    int sum = x + y;
    cout << toBase(sum, base) << endl;
}