No Rules Rules

제로 (feat. 백준, 10773번) 본문

생활/코테

제로 (feat. 백준, 10773번)

개발하는 완두콩 2022. 8. 11. 22:19
728x90
반응형

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

 

10773번: 제로

첫 번째 줄에 정수 K가 주어진다. (1 ≤ K ≤ 100,000) 이후 K개의 줄에 정수가 1개씩 주어진다. 정수는 0에서 1,000,000 사이의 값을 가지며, 정수가 "0" 일 경우에는 가장 최근에 쓴 수를 지우고, 아닐 경

www.acmicpc.net

 

반응형

 

// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <stack>
using namespace std;
int main() {
	ios::sync_with_stdio(false), cin.tie(NULL);
	stack<int> tmp;
	register int N, ans = 0;
	cin >> N;
	for (register int n = 0, v; n < N; ++n) {
		cin >> v;
		if (v != 0)
			tmp.push(v);
		else
			tmp.pop();
	}
	while (!tmp.empty())
		ans += tmp.top(), tmp.pop();
	cout << ans;
	return 0;
}
// *&)*@*

자료구조 stack을 사용하여 0을 입력받으면 pop, 아니면 push 하고 스택에 있는 모든 정수를 합하는 문제입니다.

728x90
반응형
Comments