백준 algorithm
백준 10773 - 제로
cosmohoo
2019. 9. 25. 07:46
반응형

stack 라이브러리를 쓸 줄 아는지에 대한 문제이다.
stack에 대한 개념을 알고 있어야한다. 예외처리가 필요 없는 문제라 쉬웠다.
#include <iostream>
#include <stack>
using namespace std;
int main()
{
int num; //입력받을 명령어 갯수 변수
int command; // 명령어 변수
int val=0; // 최종값 변수
cin >> num;
stack<int> stack;
for (int i = 0; i < num; i++)
{
cin >> command;
if (command == 0)
{
stack.pop();
}
else
{
stack.push(command);
}
}
while (!stack.empty())//empty면 true를 반환한다
{
val += stack.top();
stack.pop();
}
cout << val << '\n';
return 0;
}반응형