BOJ

[백준온라인저지/BOJ] 2292번 벌집

torimuk 2022. 1. 25. 13:32

문제


 

풀이


핵심은 벌집의 규칙이다. 방을 지나가는 수를 Depth라고 가정.

 

0~1 -> Depth = 1

2~7 -> Depth = 2

8~19 -> Depth = 3

20~37 -> Depth = 4

...

 

범위의 끝 숫자를 이어보면 1, 7, 18, 37, 61... 이며 6, 12, 18, 24... 만큼 값이 증가함을 알 수 있다.

따라서 6씩 증가하는 addNum 값을 더해주고, 더할 때마다 cnt 값을 증가시키면 Depth를 구할 수 있다.

단, 시작과 끝을 포함하므로 cnt는 1부터 시작한다.

 


 

import java.util.Scanner;

public class Main {
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        int tmp = 1;
        int honeycomb = 0;
        int cnt = 1;
        while(tmp < num){
            honeycomb += 6;
            tmp += honeycomb;
            cnt += 1;
        }
        System.out.println(cnt);
    }
}