Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: Quinruj

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

Language: cpp

Time: 0.002 second

Submitted On: 2025-10-05 22:00:50

#include <bits/stdc++.h>
using namespace std;

char bit8(string c){
    while (c.size() < 3) c+='0';
    reverse(c.begin(),c.end());
    int num = 0;
    if (c[0] == '1') num += 4;
    if (c[1] == '1') num += 2;
    if (c[2] == '1') num += 1;
    return num+'0';
}

string bit2(char c){
    int num;string s = "";
    if (c >= 'A' && c <= 'F') num = int(c)-55;
    else num = c-'0';
    if (num >= 8) {
        s += '1';
        num -= 8;
    }
    else s += '0';
    if (num >= 4) {
        s += '1';
        num -= 4;
    }
    else s += '0';
    if (num >= 2) {
        s += '1';
        num -= 2;
    }
    else s += '0';
    if (num >= 1) {
        s += '1';
        num -= 1;
    }
    else s += '0';
    
    return s;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    string s;cin>>s;
    string temp = "";
    for (char i : s) temp += bit2(i);
    while (!temp.empty() && *temp.begin() != '1') temp.erase(temp.begin());
    cout << temp << '\n';
    string temp1 = "",stor = "";
    for (int i = temp.size()-1,cnt = 0;i>=0;i--,cnt++) {
        if (cnt == 3) {
            stor += bit8(temp1);
            temp1 = "";
            cnt = 0;
        }
        temp1 += temp[i];
    }
    if (!temp1.empty()) stor += bit8(temp1);
    reverse(stor.begin(),stor.end());
    cout << stor << '\n';
}