반응형
문제 링크
코드
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
string s;
stack<int> ST;
int n, x;
cin >> n;
while (n--) {
cin >> s;
if (s == "push") {
cin >> x;
ST.push(x);
}
else if (s == "pop") {
if (ST.empty()) cout << -1;
else {
cout << ST.top();
ST.pop();
}
cout << '\n';
}
else if (s == "size") {
cout << ST.size() << '\n';
}
else if (s == "empty") {
cout << ST.empty() << '\n';
}
else if (s == "top") {
if (ST.empty()) cout << -1;
else cout << ST.top();
cout << '\n';
}
}
}
설명
스택을 구현하는 문제이다.
STL 스택을 사용할 수도 있지만, 직접 구현해보면 더 좋다.
(주의) 기록용으로 작성한 글입니다. 좋은 코드가 아닐 수 있습니다.
댓글 환영합니다!
반응형
'Algorithm > BOJ' 카테고리의 다른 글
[백준/BOJ] 11004번 - K번째 수 (C++) (0) | 2022.03.25 |
---|---|
[백준/BOJ] 10867번 - 중복 빼고 정렬하기 (C++) (0) | 2022.03.25 |
[백준/BOJ] 10814번 - 나이순 정렬 (C++) (0) | 2022.03.25 |
[백준/BOJ] 10773번 - 제로 (C++) (0) | 2022.03.25 |
[백준/BOJ] 10162번 - 전자레인지 (C++) (0) | 2022.03.24 |
댓글