Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: cyblox_boi

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

Language: cpp

Time: 0.003 second

Submitted On: 2025-10-17 07:30:16

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

string toBase(int n, int base)
{
    if (n == 0)
    {
        return "0";
    }

    string result = "";

    while (n > 0)
    {
        result = to_string(n % base) + result;
        n /= base;
    }

    return result;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string text;
    cin >> text;

    int number = 0;

    for (int i = text.length() - 1, j = 0; i >= 0; i--, j++)
    {
        int digit;

        if (isalpha(text[i]))
        {
            digit = text[i] - 'A' + 10;
        }
        else
        {
            digit = text[i] - '0';
        }

        number += digit * pow(16, j);
    }

    cout << toBase(number, 2) << '\n';
    cout << toBase(number, 8) << '\n';

    return 0;
}