백준 2292번 문제

풀이
- 벌집모양으로 육각형으로 이뤄져있고 한 써클 씩 신경써야 하는 문제
- 일반 수학 수열을 생각하면 쉽게 해결할 수 있는 문제
- 조건문과 반복문으로 충분히 풀 수 있는 문제
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class math5 {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(bf.readLine());
int floor = 1;
if(N > 1) {
int count1 = 1;
int count2 = 1;
while (true) {
if (N >= count1 && N <= count2) break;
count2 = count2 + (6 * floor);
count1 = count2 - (6 * floor);
floor++;
}
}
System.out.println(floor);
}
}
1. 조건문
int N = Integer.parseInt(bf.readLine());
int floor = 1;
if(N > 1) {
}
}
- 1은 기본 값 1층 이므로 그 이상의 값을 검사
2. 벌집 층
int count1 = 1;
int count2 = 1;
while (true) {
if (N >= count1 && N <= count2) break;
count2 = count2 + (6 * floor);
count1 = count2 - (6 * floor);
floor++;
}
- 벌집은 수열의 형식으로 +6 만큼 증가 중 (6, 12, 18, ...)
- num1은 한 층의 최솟 값으로 num2는 최댓 값으로 설정
- 만일 num1과 num2에 해당하는 값이 없다면 +floor
