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


[알고리즘풀이]

자료구조 스택을 선언하고, 입력에 맞춰서 명령을 실행하면 되는 문제입니다.

#include<iostream>
#include<stack>
#include<string>

using namespace std;

int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0);

	stack<int> s;
	int t;
	cin >> t;
	while (t--) {
		string input, input2;
		cin >> input;
		if (input == "push") {
			cin >> input2;
			s.push(stoi(input2));
			continue;
		}

		if (input == "pop") {
			if (s.empty()) {
				cout << "-1\n";
			}
			else{
				cout << s.top() << '\n';
				s.pop();
			}
		}
		else if (input == "size") {
			cout << s.size() << '\n';
		}
		else if (input == "empty") {
			if (s.empty())
				cout << "1\n";
			else
				cout << "0\n";
		}
		else if (input == "top") {
			if (s.empty()) {
				cout << "-1\n";
			}
			else
				cout << s.top() << '\n';
		}
		else {
			s.push((int)input[4]);
		}
	}
}

 

'Problem Solving > BOJ' 카테고리의 다른 글

[BOJ] 9012 - Parenthesis  (0) 2019.09.19
[BOJ] 10773 - Zero That Out  (0) 2019.09.19
[BOJ] 5430 - Integer Lists  (0) 2019.09.18
[BOJ] 1021 - 회전하는 큐  (0) 2019.09.17
[BOJ] 10866 - 덱  (0) 2019.09.17

+ Recent posts