[코딩 테스트 RUN] SWEA 수업 : 8931.제로

코테런/SWEA(알고리즘 수업)
2026.07.06
반응형

 

⚠️ 주의!
SWEA에서 낸 코딩 문제에 대한 해답이 들어있습니다.
열람 시 주의해주세요.

 

[SW Expert Academy] 8931.제로

 

SW Expert Academy

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

swexpertacademy.com

 

스택의 자료구조만 알면 쉬운 내용. 0이 아니라면 push를, 0이라면 앞서 있던 stack의 내용을 pop한다. 이후 stack의 내용을 더하면 된다.

 

 

정답

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        int TC = Integer.parseInt(br.readLine());

        for (int test_case = 1; test_case <= TC; test_case++) {
            int K = Integer.parseInt(br.readLine());
            Deque<Integer> stack = new ArrayDeque<>();
            for (int i = 0; i < K; i++) {
                int num = Integer.parseInt(br.readLine());
                if(num != 0) {
                    stack.push(num);
                } else {
                    stack.pop();
                }
            }

            int total = 0;
                for(int num : stack){
                    total += num;
                }

            sb.append("#").append(test_case).append(" ").append(total).append("\n");

        }

        System.out.print(sb.toString());
    }
}
반응형
«   2026/07   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31