[C++] 백준 10828. 스택

멋진감자·2025년 1월 12일
1

알고리즘

목록 보기
66/104
post-thumbnail

🌽 문제

🥕 입출력

🥔 풀이

문제에 주어진 모든 명령어가 STL에 구현되어 있다.
헤더에 stack을 추가하여 문제대로 구현하면 된다.

🥬 코드

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

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, tmp;
	string str;
	stack<int> s;

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

🥜 채점

profile
난멋져

0개의 댓글

관련 채용 정보