https://www.acmicpc.net/problem/2292
2292번: 벌집
위의 그림과 같이 육각형으로 이루어진 벌집이 있다. 그림에서 보는 바와 같이 중앙의 방 1부터 시작해서 이웃하는 방에 돌아가면서 1씩 증가하는 번호를 주소로 매길 수 있다. 숫자 N이 주어졌
www.acmicpc.net
1단계 : 1
2단계 : 2~7(=1+6)
3단계 : 8~19(=1+6+12)
4단계 : 20~37(=1+6+12+18)
...
1 ~ 7 ~ 19 ~ 37 ~ ...
1 ~ 1+6 ~ 1+6+12 ~ 1+6+12+18 ~ ...
각 단계별 마지막 숫자보다 작은지를 loop를 통해 확인하고 그 단계보다 작거나 같으면 그 단계로 출력하는 코드
#include <iostream>
using namespace std;
int main() {
int n, t=1, l=1;
cin >> n;
while(1){
if (n <= t){
cout << l;
break;
}
t += 6*l;
l++;
}
}
#include <stdio.h>
int main() {
int n, t=1, l=1;
scanf("%d",&n);
while(n > t){
t += 6*l;
l++;
}
printf("%d",l);
}
n = int(input())
t,l=1,1
while n >t:
t += 6*l
l += 1
print(l)