Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: Alif_Sama

Problemset: แปลงเลขฐาน

Language: cpp

Time: 0.003 second

Submitted On: 2025-10-08 08:53:01

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    string base16;
    cin >> base16;
    //tobase10
    int base10 = 0;
    for (int i= 0;base16[i] != '\0'; i++) {
        if (base16[i] >= 'A' && base16[i] <= 'F') {
            base10 += (base16[i] - 55) * pow(16,base16.length()-i-1);
        } else {
            base10 += (base16[i] - '0') * pow(16,base16.length()-i-1);
        }
    }
    // cout << base10 << "\n";
    //tobase2
    int base2 = 0;
    int base2arr[20];
    int tobase2 = base10;
    int i = 0;
    for(i = 0; tobase2 != 0; i++) {
        int a = tobase2 % 2;
        base2arr[i] = a;
        tobase2 /= 2;
    }
    base2arr[i] = '\0';
    for(int k = i-1; k >= 0; k--) {
        cout << base2arr[k];
    }
    cout << "\n";
    // tobase8
    int base8arr[20];
    int tobase8 = base10;
    int j = 0;
    for(j = 0; tobase8 != 0; j++) {
        int a = tobase8 % 8;
        base8arr[j] = a;
        tobase8 /= 8;
    }
    base8arr[j] = '\0';
    for(int k = j-1; k >= 0; k--) {
        cout << base8arr[k];
    }
}