Submission

Status:

PPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: Peam

Problemset: บวกเลขฐาน

Language: c

Time: 0.003 second

Submitted On: 2025-10-09 22:06:44

#include <stdio.h>

int base_n_to_base10(char a[], int base){
    int res = 0;
    for (int i = 0;a[i] != '\0';i++){
        if ('0' <= a[i] && a[i] <= '9'){
            res = base * res + (a[i] - '0');
        }
        else {
            res = base * res + (a[i] - 'A' + 10);
        }
    }
    return res;
}

int main()
{
    int base;
    scanf("%d", &base);
    char a[16], b[16];
    scanf("%s %s", a, b);
    int num_a = base_n_to_base10(a, base), num_b = base_n_to_base10(b, base);
    int num_c = num_a + num_b;
    char ans[16];
    int idx = 0;

    if(num_c == 0){
        printf("0");
        return 0;
    }
    
    int len = 0;
    // base_10_to_base_n
    while(num_c > 0){
        if(num_c % base > 9){
            ans[idx++] = (num_c % base) + 'A' - 10;
        }
        else{
            ans[idx++] = (num_c % base) + '0';
        }
        num_c /= base;
        len++;
    }

    for(int i = len - 1; i >= 0; i--){
        printf("%c", ans[i]);
    }

    return 0;
}