[BJ / 1212] 8진수 2진수

Lainlnya·2023년 4월 6일
0

BaekJoon

목록 보기
31/37

문제

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

입력

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

출력

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

예시

풀이

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let input = '';
rl.on('line', (line) => {
  input = line;
});

rl.on('close', () => {
  console.log(solution(input));
});

function solution(num) {
  let input = [...num];
  let answer = [];
  for (let i = 0; i < input.length; i++) {
    if (i !== 0) answer.push(parseInt(input[i], 8).toString(2).padStart(3, 0));
    else answer.push(parseInt(input[i], 8).toString(2));
  }
  return answer.join('');
}
profile
Growing up

0개의 댓글