본문 바로가기
IT/알고리즘

백준 2292 벌집 (수학)

by 어센트 2020. 1. 3.

1번방을 기준으로 하였을 때 둘러쌓인 방들의 개수가 한 겹씩 커질때마다 6의 배수로 증가하기 떄문에 flag를 6씩 증가시켰다. 1번방도 지나는 방에 포함시켜야함으로 ans를 1로 초기화하고 문제를 풀었다.  

 

내가 푼 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
public class 벌집 {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int flag = 6 ,start = 1;
        int ans = 1;
        while(start<N) {
            start+=flag;
            flag+=6;
            ans++;
        }
        System.out.println(ans);
        sc.close();
    }
 
}
 
cs