Baekjoon - 11286

Tadap·2023년 10월 21일
0

Baekjoon

목록 보기
60/94
post-custom-banner

문제

Solved.ac Class3++

1차시도

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		Queue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				int absO1 = Math.abs(o1);
				int absO2 = Math.abs(o2);

				if (absO1 == absO2) {
					return o1 - o2;
				}
				return absO1 - absO2;
			}
		});

		int size = Integer.parseInt(br.readLine());

		for (int i = 0; i < size; i++) {
			int data = Integer.parseInt(br.readLine());
			if (data == 0) {
				if (queue.isEmpty()) {
					sb.append(0).append("\n");
				} else {
					sb.append(queue.remove()).append("\n");
				}
			} else {
				queue.add(data);
			}
		}
		System.out.println(sb);
	}
}

성공

ToKotlin

fun main() {
    val sb = StringBuilder()
    val queue: Queue<Int> = PriorityQueue { o1, o2 ->
        val absO1 = o1.absoluteValue
        val absO2 = o2.absoluteValue
        if (absO1 == absO2) {
            o1 - o2
        } else absO1 - absO2
    }

    val size = readln().toInt()

    for (i in 0..<size) {
        val data = readln().toInt()
        if (data == 0) {
            if (queue.isEmpty()) {
                sb.append(0).append("\n");
            } else {
                sb.append(queue.remove()).append("\n");
            }
        } else {
            queue.add(data);
        }
    }
    print(sb)
}

post-custom-banner

0개의 댓글