최근 글 ✨

[SWEA] 1860 진기의 최고급 붕어빵 Java

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AV5LsaaqDzYDFAXc&categoryId=AV5LsaaqDzYDFAXc&categoryType=CODE&problemTitle=&orderBy=RECOMMEND_COUNT&selectCodeLang=ALL&select-1=3&pageSize=10&pageIndex=2

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

[코드]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

import javax.sound.midi.Soundbank;

public class CodingTest {
	static int N;
	static int M;
	static int K;
	static int[] line;
	static boolean flag;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			flag = true;
			N = sc.nextInt();
			M = sc.nextInt();
			K = sc.nextInt();
			line = new int[N];
			for (int i = 0; i < N; i++) {
				line[i] = sc.nextInt();
			}

			Arrays.sort(line); // 오름차순 정렬

			int bread = 0;
			int cur = 0;
			if (line[0] == 0) { // 0초에 오는 손님
				flag = false;
			} else {
				for (int i = 1; i <= line[line.length - 1]; i++) { // 마지막 손님 오는 시간까지
					if (i % M == 0) { // 빵 나오는 시간
						bread += K;
					}
					if (cur < N) {
						if (i == line[cur]) {
							bread--;
							cur++;

							if (bread < 0) { // 빵 다 팔림
								flag = false;
								break;
							}
						}
					}
				}
			}
			String str = flag ? "Possible" : "Impossible";

			System.out.println("#" + test_case + " " + str);
		}
	}

}

처음에는 time+=M, bread+=K로 설정하고 while문으로 처리해주려고 했는데 생각보다 복잡해져서 수정했다.

그리고 0초에 손님 오는 건 생각도 못했는데 0초에 오는 손님이 있었다;;