본문 바로가기

코딩테스트/C++

[백준 C++] 1212번: 8진수 2진수

https://www.acmicpc.net/problem/1212

 

1212번: 8진수 2진수

첫째 줄에 8진수가 주어진다. 주어지는 수의 길이는 333,334을 넘지 않는다.

www.acmicpc.net

 

 

#include <iostream>
#include <string>
using namespace std;


int main(int argc, char* argv[])
{
string str;
cin >> str;

for (int i = 0; i <str.length() ; i++) {
int a[3] = { 0, };
for (int j = 0; j < 3; j++) {
a[j] = str[i] % 2;
str[i] /= 2;
}
for (int j = 2; j >= 0; j--) {
if (i == 0) {
if (j == 2 && a[j] == 0)
continue;
if (j == 1 && a[j + 1] == 0 && a[j] == 0)
continue;
}
cout << a[j];
}
}

return 0;
}

 

극한의 하드코딩으로 작성