알고리즘 기초 - 10828번 스택(C++)
2022. 8. 12. 13:05ㆍ코딩테스트/백준
https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false); // c와 c++의 표준 입출력 스트림을 동기화를 하지 않겠다는 의미
cin.tie(nullptr); // cin사용시 출력 버퍼를 비우지(flush) 않는다.
int n;
cin >> n;
stack<int> stk;
string str;
for(int i =0; i< n; i++)
{
cin >> str;
if(str == "push")
{
int num;
cin>> num;
stk.push(num);
}else if(str == "pop")
{
if(!stk.empty())
{
cout <<stk.top()<< endl;
stk.pop();
}else
cout << "-1" << endl;
}else if(str == "size")
{
cout << stk.size() << endl;
}else if(str == "empty")
{
if(stk.empty())
{
cout << "1" <<endl;
}else
cout << "0" <<endl;
}else if(str == "top")
{
if(stk.empty())
{
cout << "-1" << endl;
}else
cout << stk.top() << endl;
}
}
return 0;
}
|
cs |
'코딩테스트 > 백준' 카테고리의 다른 글
백준 1874 스택수열 c++ (0) | 2022.08.23 |
---|---|
백준 9012 괄호 (c++) (0) | 2022.08.22 |
코딩플러스 (브루트포스) - 1476번 (C++) (0) | 2022.07.25 |
코드플러스 (브루트포스) - 3085번 (C++) (0) | 2022.07.25 |
코드플러스 기초 (브루트포스) - 2309 (C++) (0) | 2022.07.25 |