Recent Posts
Notice
No Rules Rules
제로 (feat. 백준, 10773번) 본문
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
반응형
'생활 > 코테' 카테고리의 다른 글
균형잡힌 세상 (feat. 백준, 4949번) (0) | 2022.08.12 |
---|---|
괄호 (feat. 백준, 9012번) (0) | 2022.08.11 |
스택 (feat. 백준, 10828번) (0) | 2022.08.11 |
주유소 (feat. 백준, 13305번) (0) | 2022.08.11 |
그룹 단어 체커 (feat. 백준, 1316번) (0) | 2022.08.11 |
Comments