Submission
Status:
PPPPPPPPPP
Subtask/Task Score:
100/100
Score: 100
User: pann
Problemset: แปลงเลขฐาน
Language: cpp
Time: 0.002 second
Submitted On: 2026-10-04 18:15:55
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
int main(){
string hex;
cin >> hex;
int dec = 0;
for(int i = hex.length()-1; i >= 0; i--){
int n;
if (hex[i] >= 'A' && hex[i] <= 'Z'){
n = 10+hex[i]-'A';
}
else {
n = hex[i] - '0';
}
dec += n*pow(16, hex.length()-1-i);
}
string binary = "";
int a = dec;
while (dec > 0){
binary += (char)(dec%2 + '0');
dec /= 2;
}
reverse(binary.begin(), binary.end());
cout << binary << "\n";
string octal = "";
while (a > 0){
octal += (char)(a%8 + '0');
a /= 8;
}
reverse(octal.begin(), octal.end());
cout << octal;
return 0;
}