-
백준 2292번 : 벌집알고리즘, SQL/알고리즘 2023. 12. 22. 22:35
간단하게 가운데 벌집모양부터 시작해서 인접한 변에 하나씩 추가되는 육각형에, 입력되는 숫자가 간운데로부터 몇번째에 접해있는지 찾는 문제이다!
육각형 하나를 방(room)이라고 생각했을 때, 가운데 1번방과 접해있는 가장 큰 방이 7이다.
7번방과 접해있는 가장 큰 방은 19번, 19번과 접해있는 가장 큰 방은 37번...
즉 1, 7, 19, 37, 61로 6의 배수만큼 커지는 것을 알 수 있다.
코드를 생성해보면..
public class Main { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num = Integer.parseInt(br.nextLine()); int count = 1; int room = 1; while(true) { if(num<=room) { //가장 큰 room의 값과 비교해서 작다면 반복문을 나온다. break; } room =room+(6*count); //가장 큰 방의 값은 6의 배수만큼 커진다. count++; } System.out.println(count); } }
위와 같이 풀 수 있다.
'알고리즘, SQL > 알고리즘' 카테고리의 다른 글
그리디(Greedy) 알고리즘이란? (+ 백준 5585번 : 거스름돈) (0) 2023.12.26 백준 2775번 : 부녀회장이 될테야 (0) 2023.12.23 이분 탐색/이진 탐색(Binary Search)이란? (+백준 1920번) (0) 2023.12.15 백준 2751번 : 수 정렬하기2 (+System.out.println 반복사용의 단점) (0) 2023.12.13 백준 7568번 : 덩치 (1) 2023.12.12