Stack으로 풀수있는 간단한 문제 같아 보였지만 런타임 에러에 게속해서 막혔다. 런타임에러는 예외처리를 해줘야 해결이 된다.
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main()
{
int N;
int num;
stack<int> stacks;
cin >> N;
string inputString;
for (int i = 0; i < N; i++)
{
cin >> inputString;
if (inputString == "push")
{
cin >> num;
stacks.push(num);
}
else if (inputString == "pop")
{
if (stacks.empty() == true)
{
cout << -1 << endl;;
}
else
{
cout << stacks.top() << endl;
stacks.pop();
}
}
else if (inputString == "size")
{
cout << stacks.size() << endl;
}
else if (inputString == "empty")
{
if (stacks.empty() == true)
{
cout << 1 << endl;
}
else
{
cout << 0 << endl;
}
}
else if (inputString == "top")
{
if (stacks.empty() == true)
{
cout << -1 << endl;
}
else
{
cout << stacks.top() << endl;
}
}
}
return 0;
}