반응형
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int k; //k의 수 만큼 입력받는다
int sum = 0;
int num;
stack<int> s; //stack 선언
cin >> k; //k입력
for (int i = 0; i < k; i++) //k만큼 반복
{
cin >> num; //num입력
if (num != 0) //num이 0 이아니면 스택에 num을 추가
s.push(num);
else
s.pop(); //num이 0이면 스택의 top 데이터 삭제
}
while (!s.empty()) //스택이 empty가 아니면 반복
{
sum += s.top(); //스택의 데이터를 sum에 더하고
s.pop(); //삭제
}
cout << sum;
return 0;
}
반응형
'Programming > Baekjoon' 카테고리의 다른 글
백준 11047 동전0 [c++] (0) | 2022.01.17 |
---|---|
백준 5800 성적 통계 [c++] (0) | 2022.01.17 |
백준 1427 소트인사이드 [c++] (0) | 2022.01.17 |
백준 1037 약수 [c++] (0) | 2022.01.17 |
백준 2822 점수계산 [c++] (0) | 2022.01.16 |