![[백준 / BOJ] 2292번 벌집 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FddeLhz%2FbtrJilOszdR%2Fz8xuIPEs6ay5H5sFwkNY3k%2Fimg.png)
[백준 / BOJ] 2292번 벌집 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 1. 18. 10:14
Table of Contents
반응형
링크 : https://www.acmicpc.net/problem/2292
2292번: 벌집
위의 그림과 같이 육각형으로 이루어진 벌집이 있다. 그림에서 보는 바와 같이 중앙의 방 1부터 시작해서 이웃하는 방에 돌아가면서 1씩 증가하는 번호를 주소로 매길 수 있다. 숫자 N이 주어졌
www.acmicpc.net
문제

문제 풀이
가장 중앙 방, 즉 1번 레이어는 1, 2번 레이어부터 2~7, 3번 레이어는 8~19, 4번 레이어는 20~36..
즉 6, 12, 18, 24의 등비수열대로 늘어나는 패턴을 확인할 수 있다. 이를 활용해 등비수열의 합을 구하고 늘어난 레이어를 출력해주면 된다.
C++ 상세 풀이
C++ 풀이 펼쳐보기
void solution(int N) {
int cnt = 1;
int layer = 1;
while (N > cnt) {
cnt += 6 * layer;
layer++;
}
cout << layer;
}
위에서 cnt는 방 갯수를 세기 위한 변수이고, layer은 층을 세기 위한 변수이다. 즉, 결국 layer을 출력해야 한다.
일단 둘 다 1로 초기화한 후, 입력받은 N이 cnt보다 클 동안 등비수열을 이용해 layer을 점점 증가시켜 주어 어느 층인지 판별한다.
Python 상세 풀이
Python 풀이 펼쳐보기
N = int(input())
cnt = 1
layer = 1
while N > cnt :
cnt += 6 * layer
layer += 1
print(layer)
위에서 cnt는 방 갯수를 세기 위한 변수이고, layer은 층을 세기 위한 변수이다. 즉, 결국 layer을 출력해야 한다.
일단 둘 다 1로 초기화한 후, 입력받은 N이 cnt보다 클 동안 등비수열을 이용해 layer을 점점 증가시켜 주어 어느 층인지 판별한다.
C++ 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
void solution(int N) | |
{ | |
int layer = 0; | |
if (N == 1) | |
cout << 1; | |
else | |
{ // N이 1일때는 위의 if문에서 걸러지므로 2부터 시작 | |
for (int i = 2; i <= N; ++layer) | |
i += 6 * layer; | |
cout << layer; | |
} | |
} | |
int main() | |
{ | |
int N; | |
cin >> N; | |
solution(N); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
void solution(int N) | |
{ | |
int cnt = 1; | |
int layer = 1; | |
while (N > cnt) | |
{ | |
cnt += 6 * layer; | |
layer++; | |
} | |
cout << layer; | |
} | |
int main() | |
{ | |
int N; | |
cin >> N; | |
solution(N); | |
return 0; | |
} |
Python 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
N = int(input()) | |
cnt = 1 | |
layer = 1 | |
while N > cnt : | |
cnt += 6 * layer | |
layer += 1 | |
print(layer) |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 2869번 달팽이는 올라가고 싶다 (C++, Python) (0) | 2022.02.01 |
---|---|
[백준 / BOJ] 1193번 분수찾기 (C++, Python) (0) | 2022.02.01 |
[백준 / BOJ] 1712번 손익분기점 (C++, Python) (0) | 2022.01.18 |
[백준 / BOJ] 1316번 그룹 단어 체커 (C++, Python) (0) | 2022.01.17 |
[백준 / BOJ] 2941번 크로아티아 알파벳 (C++, Python) (0) | 2022.01.17 |
@Reo :: 코드 아카이브
자기계발 블로그