Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

NY's 개발일기

[백준] 스택(10828번) - C++ 본문

Study/Algorithm

[백준] 스택(10828번) - C++

developer_ny 2021. 4. 17. 03:58

문제

입력

출력

소스 코드

#include <iostream>
#include <stack>

using namespace std;

int main() {
	int n; cin >> n;
	stack<int> s;
	for(int i=0;i<n;i++) {
		string command; cin >> command;
		if(command == "push") {
			int num; cin >> num;
			s.push(num);
		}
		else if(command == "pop") {
			if(s.empty()) cout << -1 << "\n";
			else {
				cout << s.top() << "\n";
				s.pop();
			}
		}
		else if(command == "size") cout << s.size() << "\n";
		else if(command == "empty") {
			if(s.empty()) cout << 1 <<"\n";
			else cout << 0 << "\n";
		}
		else {
			if(s.empty()) cout << -1 << "\n";
			else {
				cout << s.top() << "\n";
			}
		}
	}
	return 0;
}