[BOJ10828] 스택 C++

문지영·2023년 5월 7일
0

CODINGTEST C++

목록 보기
1/18

문제

스택

코드

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

stack<int> S;

int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int N;
	cin >> N;
	while (N--) {
		string oper;
		cin >> oper;
		if (oper == "size") {
        	cout << S.size() << '\n';
		}
		else if (oper == "empty") {
			cout << (int)S.empty() << '\n';
		}
		else if (oper == "top") {
			int top = -1;
			if (!S.empty()) top = S.top();
            cout << top << '\n';
		}
		else if (oper == "push") {
			int n;
			cin >> n;
			S.push(n);
		}
		else {// pop
			int n = -1;
			if (!S.empty()) {
				n = S.top();
				S.pop();
			}
			cout << n << '\n';
		}
	}
}

제출

STL stack 참고

profile
BeHappy

0개의 댓글