개발일지/알고리즘

백준 10828번 : 스택

김진우 개발일지 2025. 1. 19. 15:51
#include<iostream>
#include<stack>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
	int N;
	scanf("%d", &N);
	cin.ignore();
	stack<int> myStack;
	for (int i = 0; i < N; i++)
	{
		string params;
		getline(cin, params);
		
		int blankIndex = params.find(" ");
		string param1 = params.substr(0, blankIndex);
		
		if (param1.compare("push") == 0)
		{
			int number = stoi(params.substr(blankIndex + 1, params.length()));
			myStack.push(number);
		}
		else if (param1.compare("pop") == 0)
		{
			if (myStack.empty())
			{
				printf("-1\n");

			}
			else
			{
				int number = myStack.top();
				printf("%d\n", number);
				myStack.pop();
			}
		}
		else if (param1.compare("size") == 0)
		{
			printf("%d\n", myStack.size());
		}
		else if (param1.compare("empty") == 0)
		{
			if(myStack.empty())
			{
				printf("1\n");
			}
			else
			{
				printf("0\n");
			}
		}
		else if (param1.compare("top") == 0)
		{
			if (myStack.empty())
			{
				printf("-1\n");
			}
			else
			{
				printf("%d\n", myStack.top());
			}
		}
	}
	return 0;
}

'개발일지 > 알고리즘' 카테고리의 다른 글

백준 10989번 : 덱  (0) 2025.01.20
백준 10845번 : 큐  (0) 2025.01.20
백준 10815번 : 숫자 카드  (0) 2025.01.19
백준 10989번 : 수 정렬하기 3  (0) 2025.01.18
백준 2751번 : 수 정렬하기 2  (0) 2025.01.18