

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 |
'IT > 알고리즘' 카테고리의 다른 글
백준2869 달팽이는 올라가고싶다(수학) (2) | 2020.01.04 |
---|---|
백준 1193번 분수찾기(수학) (0) | 2020.01.03 |
프로그래머스 lv2 가장 큰 수 (0) | 2019.11.19 |
프로그래머스 lv2 프린터 (0) | 2019.11.19 |
프로그래머스 lv2 다리를 지나는 트럭 (0) | 2019.11.16 |