본문 바로가기
Algorithm

백준알고리즘 1094번 막대기

by jayden-lee 2019. 4. 22.
728x90

1094번 막대기 문제는 알고리즘 유형이 수학 또는 시뮬레이션에 해당하는 문제입니다.

import java.util.Scanner;
/**
 * 막대기 문제<br>
 * 알고리즘 유형 : 수학, 시뮬레이션
 * 
 * @author jayden-lee
 */
public class Main {
    // 처음 막대 크기
    private static int bar_length = 64;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // Xcm 막대 크기
        int x = scanner.nextInt();
        // 막대 개수
        int bar_count = 0;
        while (x > 0) {
            if (bar_length > x) {
                bar_length /= 2;
            } else {
                bar_count++;
                x -= bar_length;
            }
        }
        System.out.println(bar_count);
        scanner.close();
    }
}

댓글