티스토리 뷰

백준

백준 소스코드 [C++] 10828 스택

Hani_Levenshtein 2020. 9. 7. 23:13

www.acmicpc.net/problem/10828

 

10828번: 스택

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

www.acmicpc.net

백준 소스코드 [C++] 10828 스택

#include <iostream>
#include <stack>
#include<string>
using namespace std;

int main() {
	int n,m;
	string x;
	cin >> n;
	stack<int> st;
	for (int i = 0;i < n;i++) {
		cin >> x;
		if (x.compare("push") == 0) {
			cin >> m;
			st.push(m);
		}
		if (x.compare("pop") == 0) {
			if (st.empty()) cout << "-1" << '\n';
			else {
				cout << st.top() << '\n';
				st.pop();
			}
		}
		if (x.compare("size") == 0) cout<<st.size()<<'\n';
		if (x.compare("empty") == 0) {
			if (st.empty()) cout << "1" << '\n';
			else cout << "0" << '\n';
		}
		if (x.compare("top") == 0) {
			if (st.empty())cout << "-1" << '\n';
			else cout << st.top() << '\n';
		}
	}

	return 0;
}
댓글