Posts [BOJ 백준] 10773번 제로 / C++
Post
Cancel

[BOJ 백준] 10773번 제로 / C++


Contents



문제


문제를 보시려면 링크를 클릭해주세요.


풀이


STL stack을 사용하여 풀었습니다.

입력값이 0 or 나머지 숫자 인 경우만 나눠서 간단하게 풀 수 있는 문제입니다.


소스 코드


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
32
33
34
35
36
37
38
#include <iostream>
#include <stack>


using namespace std;

int main(int argc, char**argv) {

	ios::sync_with_stdio(0);
	cin.tie(0);
		
	stack<int> S;
	int K;
	int sum=0;
	
	cin >> K;
	
	while(K--){
		int N;
		cin >> N;
		
		if(N){
			S.push(N);
			sum+=N;
		}
		
		else{
			if(!S.empty()){
				sum-=S.top();
				S.pop();
			}
		}
	}
	
	cout << sum;
	
	return 0;
}



This post is licensed under CC BY 4.0 by the author.

[BOJ 백준] 10828번 스택 / C++

[WebLogic] 웹로직 콘솔 특정 ip 접속 제한 방법

Comments powered by Disqus.