๐Ÿ“Œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ค€๋น„ :: ๋ฐฑ์ค€_10773:: ์Šคํƒ&ํ - ์ œ๋กœ๐Ÿ‘€

Dev-Oยท2022๋…„ 2์›” 17์ผ
0

CodingTest

๋ชฉ๋ก ๋ณด๊ธฐ
14/18

๋ฌธ์ œ

ํ’€์ด

์Šคํƒ์— ์ˆซ์ž๋ฅผ ๊ณ„์† ์ง‘์–ด๋„ฃ๋‹ค๊ฐ€ 0์ด๋ผ๋Š” ์ˆซ์ง€๊ฐ€ ๋‚˜์˜ค๋ฉด ์Šคํƒ์—์„œ ์ˆซ์ž๋ฅผ ๋บด์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ ์ž…๋‹ˆ๋‹ค.

์ฝ”๋“œ

package algorithm.stack_queue;

import java.util.Scanner;
import java.util.Stack;

public class Bj10773_zero {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Stack<Integer> budget = new Stack<>();
		int n = sc.nextInt();
		int sum = 0;
		for (int i = 0; i < n; i++) {
			int money = sc.nextInt();
			
			if(money!= 0) budget.push(money);
			else budget.pop();	
		}
		for(int money : budget) {
			sum += money;
		}
		System.out.println(sum);
	}

}
profile
Being Outstanding needs Understanding๐Ÿš€

0๊ฐœ์˜ ๋Œ“๊ธ€