Notice
Recent Posts
Recent Comments
Link
궤도
[백준] 10773번 : 제로 본문
문제
풀이
입력을 계속 스택에 push로 쌓다가 0을 만나면 pop해주면 되겠다.
입력이 끝나면 스택에 남아있는 모든 수를 더해주자.
소스코드
#include <iostream>
#include <stack>
using namespace std;
int main() {
int K, input, sum = 0;
stack<int> s;
cin >> K;
for (int i = 0; i < K; i++) {
cin >> input;
if (input == 0) //0이면 가장 위에 있던 수 pop으로 뺌
s.pop();
else
s.push(input);
}
while (!s.empty()) { //스택에 쌓인 모든 수 더해줌
sum += s.top();
s.pop();
}
cout << sum;
}
Comments