c++

백준 c++ 28278 문제 풀이

psb08 2025. 5. 21. 08:22
728x90
반응형

문제 링크 : https://www.acmicpc.net/problem/28278

목차

1. 설명

 

2. 내 코드

 

3. 실행 결과

 

설명

백준 실버 4 문제 입니다.

문제명 : 스택 2

 

내 코드

#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<string>

using namespace std;

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

	int t, t1;
	string str;
	cin >> t;
	stack<int> stack;

	while (t--)
	{
		cin >> str;

		if (str == "1")
		{
			cin >> t1;
			stack.push(t1);
		}

		else if (str == "2")
		{
			if (!stack.empty())
			{
				cout << stack.top() << '\n';
				stack.pop();
			}
			else
				cout << -1 << '\n';
		}

		else if (str == "3")
		{
			cout << stack.size() << '\n';
		}

		else if (str == "4")
		{
			(stack.empty() ? cout << 1 << '\n' : cout << 0 << '\n');
		}

		else if (str == "5")
		{
			if (!stack.empty())
			{
				cout << stack.top() << '\n';
			}
			else
				cout << -1 << '\n';
		}
		
	}



}

 

 

실행 결과