Submission
Status:
PPPPPPPPPP
Subtask/Task Score:
100/100
Score: 100
User: Krovmoroz
Problemset: แปลงเลขฐาน
Language: cpp
Time: 0.002 second
Submitted On: 2025-11-04 18:05:00
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string hex;
cin >> hex;
// ตารางแปลง 0-F → binary 4 บิต
const string binTable[16] = {
"0000","0001","0010","0011",
"0100","0101","0110","0111",
"1000","1001","1010","1011",
"1100","1101","1110","1111"
};
bool started = false;
for (char c : hex) {
int v = (c <= '9') ? c - '0' : toupper(c) - 'A' + 10;
string bits = binTable[v];
if (!started) {
size_t pos = bits.find('1');
if (pos == string::npos) continue; // ทั้ง 4 บิตเป็นศูนย์
bits = bits.substr(pos);
started = true;
}
cout << bits;
}
if (!started) cout << 0;
// พิมพ์ฐาน 8 แบบรวดเดียวด้วย printf
unsigned int x;
sscanf(hex.c_str(), "%x", &x);
printf("\n%o", x);
}