백준 28278 c++

magicdrill·2024년 4월 18일
0

백준 문제풀이

목록 보기
328/655

백준 28278 c++

#include <iostream>
#include <stack>

using namespace std;

int input(int lower, int upper);

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int N;
	stack<int> testStack;
	int i;
	int instruction, X;

	N = input(1, 1000000);

	for (i = 0; i < N; i++)
	{
		instruction = input(1, 5);
		//switch (instruction)
		//{
		//case 1: //정수 X를 스택에 넣는다.
		//	X = input(1, 100000);
		//	testStack.push(X);
		//	break;
		//case 2://스택에 정수가 있다면 맨 위의 정수를 빼고 출력한다.
		//	if (testStack.empty())
		//	{
		//		cout << "-1" << "\n";
		//	}
		//	else
		//	{
		//		cout << testStack.top() << "\n";
		//		testStack.pop();
		//	}
		//	break;
		//case 3://스택에 들어있는 정수의 개수를 출력한다.
		//	cout << testStack.size() << "\n";
		//	break;
		//case 4://스택이 비어있으면 1, 아니면 0출력
		//	if (testStack.empty())
		//	{
		//		cout << "1" << "\n";
		//	}
		//	else
		//	{
		//		cout << "0" << "\n";
		//	}
		//	break;
		//case 5://스택에 정수가 있다면 맨 위의 정수를 출력한다.
		//	if (testStack.empty())
		//	{
		//		cout << "-1" << "\n";
		//	}
		//	else
		//	{
		//		cout << testStack.top() << "\n";
		//	}
		//	break;
		//}
		if (instruction == 1)
		{
			X = input(1, 100000);
			testStack.push(X);
		}
		else if (instruction == 2)
		{
			if (testStack.empty())
			{
				cout << "-1" << "\n";
			}
			else
			{
				cout << testStack.top() << "\n";
				testStack.pop();
			}
		}
		else if (instruction == 3)
		{
			cout << testStack.size() << "\n";
		}
		else if (instruction == 4)
		{
			if (testStack.empty())
			{
				cout << "1" << "\n";
			}
			else
			{
				cout << "0" << "\n";
			}
		}
		else
		{
			if (testStack.empty())
			{
				cout << "-1" << "\n";
			}
			else
			{
				cout << testStack.top() << "\n";
			}
		}
	}

	return 0;
}

int input(int lower, int upper)
{
	//cout << "input" << endl;
	int A;

	while (1)
	{
		cin >> A;
		if (A >= lower && A <= upper)
		{
			break;
		}
		else
		{
			;
		}
	}

	return A;
}

0개의 댓글