본문 바로가기
Java/AlgorithmPS

백준) 과제는 끝나지 않아(17952번)

by NH_club 2023. 9. 23.
 

17952번: 과제는 끝나지 않아!

성애는 이번 학기에 전공을 정말 많이 듣는다. 이로 인해 거의 매일을 과제를 하면서 보내고 있다. 그런데도 과제가 줄어들 기미가 보이지 않는데, 바로 분단위로 과제가 추가되고 있기 때문이

www.acmicpc.net

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

public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Stack<Integer> remainingTime = new Stack<>();
        Stack<Integer> score = new Stack<>();
        int sum = 0;

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

        for (int i = 0; i < n; i++) {
            String[] str = br.readLine().split(" ");
            if (remainingTime.isEmpty() && str[0].equals("0")) {
                continue;
            }
            if (str[0].equals("0")) {
                int count = remainingTime.pop();
                remainingTime.push(count - 1);
            } else {
                score.push(Integer.parseInt(str[1]));
                remainingTime.push(Integer.parseInt(str[2])-1);
            }
            if (remainingTime.peek() == 0) {
                sum += score.pop();
                remainingTime.pop();
            }
        }
        System.out.println(sum);
    }
}

깨달은 점:

배열을 Arrays.toString으로 간편하게 값을 출력할 수 있음

Stack의 top 값은 수정이 불가능 하기에 pop과 push를 다시 함으로써 top값이 수정된 결과를 얻을 수 있음

String.split으로 잘린 문자열을 배열로 받아올 수 있음

'Java > AlgorithmPS' 카테고리의 다른 글

백준) 좋은 단어(3986번) - 복습 필요  (0) 2023.09.23
백준) 단어 뒤집기2(17413번)  (0) 2023.09.23
백준) 스택 수열(1874번)  (0) 2023.09.22
백준) 세로읽기(10798번)  (0) 2023.09.20
백준) 팰린드롬수(1259번)  (0) 2023.09.20