2292번 벌집

Day8 8단계 20231026


풀이 1

방 번호 떨어진 거리
1 0
2 ~ 7 1
8 ~ 19 2
20 ~ 37 3
38 ~ 61 4
62 ~ 5
int sum = 2; // 초기값
int count = 1; // 순번(곧 1번 방으로부터 떨어진 거리)
while (true) {
	sum = sum + 6 * count;
	count++;
}
import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		if (n == 1) {
			System.out.println(1);
			return;
		}
		int sum = 2;
		int count = 1; // 1번 방에서 1칸 밖으로 나간 범위부터 시작
		while(true) {
			if (sum > n) {
				break;
			}
			sum = sum + 6 *count;	
			count++;
		}
		System.out.println(count);
		br.close();
	}
}

풀이 2

import java.io.*;

public class Main {
    public static void main(String[] args) {
        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            int roomNumber = Integer.parseInt(reader.readLine());

            int sum = 0;
            for(int i = 0; i <= roomNumber/6 + 1; i++) {
                sum += i;
                int end = 6 * sum + 1;
                if (end >= roomNumber) {
                    System.out.println(i+1);
                    return;
                }
            }
        } catch (Exception e) {
        }
    }
}