백준_10773_제로

임정민·2023년 1월 31일
3

알고리즘 문제풀이

목록 보기
32/173
post-thumbnail

코딩테스트 연습 스터디 진행중 입니다. ✍✍✍
Notion : https://www.notion.so/1c911ca6572e4513bd8ed091aa508d67

문제

https://www.acmicpc.net/problem/10773

[나의 풀이]

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

public class Main {
	
    // static 멤버로 Stack 선언
    static Stack stack1 = new Stack<>();
    
    public static void main(String[] args) throws NumberFormatException, IOException {
    
    	// 입력 받기
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int K = Integer.parseInt(br.readLine());
        
        // 요소들 push
        // 0이면 이전 요소 1개 pop
        int input;
        for(int i=0;i<K;i++){
            input = Integer.parseInt(br.readLine());
            if(input==0){
                stack1.pop();
                continue;
            }
            stack1.push(input);
        }
	
    	// stack의 모든 원소 합 출력
        int sum = 0;
        while(!stack1.isEmpty()){
            sum+=(int)stack1.pop();
        }
		
        // bw는 String으로 write 해야함
        bw.write(sum+"");
        bw.flush();

    }    
}

Stack 자료구조 문제입니다!
스터디 팀원들과 자료구조 개념을 짚어보고 싶어 풀어봤습니다!

감사합니다!🌞🌞🌞

profile
https://github.com/min731

1개의 댓글

comment-user-thumbnail
2023년 2월 2일

👍

답글 달기