백준 10828

윤재학·2022년 8월 2일

백준

목록 보기
7/8

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;
}
profile
노력하자 즐겁게 개발할수 있는 환경을 위해

0개의 댓글