algorithm codes/baekjoon online judge

10828번: 스택 (백준 온라인 저지, C++)

mimizzang 2019. 4. 15. 21:47

코드

 

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
#include <iostream>
#include <string>
 
using namespace std;
 
int stck[10001];
 
int main() {
 
    string s;
    int n, x;
    int t = -1;    //top index
    cin >> n;
 
    while (n--) {
        cin >> s;
 
        if (s == "push") {
            cin >> x;
            t++;
            stck[t] = x;
        }
        else if (s == "pop") {
            if (t == -1) {
                cout << -1 << '\n';
            }
            else {
                cout << stck[t] << '\n';
                t--;
            }
        }
        else if (s == "size") {
            cout << t + 1 << '\n';
        }
        else if (s == "empty") {
            if (t == -1) {
                cout << 1 << '\n';
            }
            else {
                cout << 0 << '\n';
            }
        }
        else if (s == "top") {
            if (t == -1) {
                cout << -1 << '\n';
            }
            else {
                cout << stck[t] << '\n';
            }
        }
    }
 
    return 0;
}

 

 

제출 결과

 

 

 

문제 출처

 

https://www.acmicpc.net/problem/10828

 

10828번: 스택

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.

www.acmicpc.net