[백준 3460 자바] 이진수

일단 해볼게·2023년 3월 17일
0

백준

목록 보기
105/132

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

import java.io.*;

public class Main {
    private static void solution() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        int T = Integer.parseInt(br.readLine()); // 테스트 케이스

        for(int i = 0; i < T; i++) {
            int n = Integer.parseInt(br.readLine());
            String nToBinary = Integer.toBinaryString(n); // 이진수로 변환

            for(int j = nToBinary.length() - 1; j >= 0; j--) { // 위치가 낮은것 부터 출력하기 위해 j--
                if(nToBinary.charAt(j) == '1') { // j 위치에서 1일 때
                    sb.append(nToBinary.length() - j - 1).append(" ");
                }
            }
            System.out.println(sb);
            sb.setLength(0); // StringBuilder 초기화
        }

    }
    public static void main(String[] args) throws Exception {
        solution();
    }
}
  • String str = Integer.toBinaryString(n); 이진수로 변환하는 대신 String 값으로 나온다.
profile
시도하고 More Do하는 백엔드 개발자입니다.

0개의 댓글