🚀 들어가며...
- 백준 10828번 문제 풀이다.
- 단순한 구현 문제
🔗 문제
https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
💌 소스코드
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main(void){
int n;
string cmd;
stack<int> st;
cin >> n;
while(n > 0){
cin >> cmd;
if(cmd=="push"){
int num;
cin >> num;
st.push(num);
}
else if (cmd=="top"){
if(st.empty())
cout << -1 << endl;
else
cout << st.top() << endl;
}
else if (cmd=="size"){
cout << st.size() << endl;
}
else if (cmd=="empty"){
if(st.empty())
cout << 1 << endl;
else
cout << 0 << endl;
}
else if (cmd=="pop"){
if(st.empty()){
cout << -1 << endl;
}
else{
cout << st.top() << endl;
st.pop();
}
}
n--;
}
}
🙋🏻♂️ 후기
전혀 어려운 내용이 없기 때문에, 구현에만 집중하면 된다.
'알고리즘 > 백준' 카테고리의 다른 글
[백준 9012] 괄호 (cpp, stack) (0) | 2021.12.12 |
---|---|
[백준 9093] 단어 뒤집기 (cpp, stack) (0) | 2021.12.12 |
[백준 1764] 듣보잡 (0) | 2019.05.17 |
[백준 7785] 회사에 있는 사람 (0) | 2019.05.17 |
댓글