[boj] (b2) 2292 벌집

강신현·2022년 4월 1일
0

✅ 구현

문제

링크

풀이

벌집의 중간 다음 막부터 layer1이라고 하면

중심 : 1 (1)
layer1 : 2 ~ 7 (6)
layer2 : 8 ~ 19 (12)
layer3 : 20 ~ 37 (18)

layer마다 들어갈 수 있는 최대 숫자를 구하고 N이 그 안에 있는지 판단하면 된다.
(N이 1일때는 layer 범주에 들지 않으므로 따로 출력)

코드

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int N, layer=0;
    cin >> N;

    if(N==1){
        cout << 1 << "\n";
        return 0;
    }

    while(1)
    {
        layer ++;

        int max=1;
        for(int j=1;j<=layer;j++){
            max += 6*j;
        }
        if(N <= max) break;
    }

    cout << layer+1 << "\n";

    return 0;
}
profile
땅콩의 모험 (server)

0개의 댓글