728x90
https://www.acmicpc.net/problem/1212
1212번: 8진수 2진수
첫째 줄에 8진수가 주어진다. 주어지는 수의 길이는 333,334을 넘지 않는다.
www.acmicpc.net
8진수를 2진수로 변환하기
수가 0인 경우를 제외하고 1로 시작해야 한다.
💡 8진수 한 자리당 2진수 3자리로 표현해야 한다.
문자열을 사용해서 하면 되는데 여기서 2진수로 바뀔 때 꼭 뒤집어야 한다는 것을 잊으면 안 된다!
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string changeTo2(char ch){
int x = ch - '0';
string two = "";
if(x == 0) return "0";
while(x != 0){
two += to_string(x%2);
x /= 2;
}
reverse(two.begin(), two.end());
return two;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
string str;
cin >> str;
for(int i=0; i<str.size(); i++){
if(i==0) cout << changeTo2(str[i]);
else{
string tmp = changeTo2(str[i]);
while(tmp.size() != 3)
tmp = '0' + tmp;
cout << tmp;
}
}
return 0;
}
728x90
'알고리즘 > 백준' 카테고리의 다른 글
[백준/c++] 5597번: 과제 안 내신 분..? (0) | 2022.05.30 |
---|---|
[백준/c++] 20053번: 최소, 최대 2 (0) | 2022.05.30 |
[백준/c++] 5639번: 이진 검색 트리 (0) | 2022.05.27 |
[백준/c++] 1068번: 트리 (0) | 2022.05.27 |
[백준/c++] 9934번: 완전 이진 트리 (0) | 2022.05.19 |