[백준] 1212번 8진수 2진수

반응형
반응형

문제

8진수가 주어졌을 때, 2진수로 변환하는 프로그램을 작성하시오.

입력

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

출력

첫째 줄에 주어진 수를 2진수로 변환하여 출력한다. 수가 0인 경우를 제외하고는 반드시 1로 시작해야 한다.

 

 

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  string o;
  cin >> o;

  if (o == "0") {
    cout << 0;
    return 0;
  }
  int index = 0;
  for(auto c : o) {
  int temp = c - '0';
  string sleep = "";
  if (temp > 0) {
    index++;
  }

  if (index > 0 && temp == 0) {
    sleep+="000";
  }
  while ( temp > 0) {
    sleep += to_string(temp%2);
    temp/=2;
  }
  reverse(sleep.begin(),sleep.end());
  if (index > 1) {
    while(sleep.length() % 3 != 0) {
      sleep = "0"+sleep;
    }
  }
    cout << sleep;
  }

}
 

게시글 체우기..

반응형

'알고리즘 > 백준' 카테고리의 다른 글

[백준] 9086번 문자열  (0) 2020.04.30
[백준] 2089번 -2진수  (0) 2020.04.29
[백준] 1373번 2진수 8진수  (0) 2020.04.24
[백준] 11722번 가장 긴 감소하는 부분 수열  (0) 2020.04.21
[백준]9465번 스티커  (0) 2020.04.20

댓글

Designed by JB FACTORY