Algorithm/BOJ

백준 10828번 C++

poopooreum 2023. 9. 10. 12:25
반응형

✏️ 문제 링크

https://www.acmicpc.net/problem/10828

 

10828번: 스택

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

 

✏️ 문제 설명

 

✏️ 문제 코드

#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main() {
	int num;
	stack<int>s;
	cin >> num;
	for (int x = 0; x < num; x++) {
		string input;
		cin >> input;
		if (input == "pop") {
			if (s.empty() == true)
				cout << -1 << endl;
			else {
				cout << s.top() << endl;
				s.pop();
			}
		}
		else if (input == "top") {
			if (s.empty() == true)
				cout << -1 << endl;
			else {
				cout << s.top() << endl;
			}
		}
		else if (input == "size")
			cout << s.size() << endl;
		else if (input == "empty")
			cout << s.empty() << endl;
		else {
			int n;
			cin >> n;
			s.push(n);
		}
	}
}

 

반응형