[백준] 10773 - 제로

seunghyun lee·2022년 7월 24일
0

Computer Science

목록 보기
12/19
post-thumbnail

문제로 가는 링크: https://www.acmicpc.net/problem/10773
등급: 실버4
알고리즘 분류: 구현, 자료 구조, 스택

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

class Stack {
    int[] arr = new int[100000];
    int top;
    int sum;
    int size;

    Stack(){
        this.top = -1;
    }
    void push(int data) {
        size++;
        arr[++top] = data;
        sum+=data;
    }
    void zero() {
        size--;
        sum-=arr[top--];
    }

    void getSum(){
        System.out.println(sum);
    }
}
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int repeat = Integer.parseInt(br.readLine());

        Stack stack = new Stack();
        while(repeat-->0) {
            int num = Integer.parseInt(br.readLine());
            if(num == 0) {
                stack.zero();
            } else {
                stack.push(num);
            }
        }
        stack.getSum();
    }
}

0개의 댓글