문제출처 : https://www.acmicpc.net/problem/10828
code
#include <iostream>
#include <stack>
using namespace std;
int main()
{
int N, push_num;
cin >> N;
stack<int>stk;
string cmd;
for (int i = 0; i < N; i++)
{
cin >> cmd;
if (cmd == "push")
{
cin >> push_num;
stk.push(push_num);
}
else if (cmd == "pop")
{
if (stk.empty())
cout << -1 << endl;
else
{
int x = stk.top();
stk.pop();
cout << x << endl;
}
}
else if (cmd == "size")
cout << stk.size() << endl;
else if (cmd == "empty")
{
if (stk.empty())
cout << 1 << endl;
else
cout << 0 << endl;
}
else if (cmd == "top")
{
if (stk.empty())
cout << -1 << endl;
else
cout << stk.top() << endl;
}
}
return 0;
}
단순 스택구현문제였다.