내가 작성한 코드

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
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
 
using namespace std;
 
int main(void) {
    int N,tmp;
    vector <int> arr;
    string S;
    scanf("%d"&N);
 
    for (int i = 0; i < N; i++) {
        cin >> S;
        if (!S.compare("push")) {
            cin >> tmp;
            arr.push_back(tmp);
        }
        else if (!S.compare("pop")) {
            if (arr.empty()) {
                printf("-1\n");
            }
            else {
                printf("%d\n", arr.back());
                arr.pop_back();
            }
        }
        else if (!S.compare("size")) {
            printf("%d\n", arr.size());
        }
        else if (!S.compare("empty")) {
            printf("%d\n", arr.empty());
        }
        else if (!S.compare("top")) {
            if (arr.empty()) {
                printf("-1\n");
            }
            else {
                printf("%d\n", arr.back());
            }
        }
    }
}
cs
  • compare을 이용하여 명령어를 판단하고 조건에 따라 나누어 작성하였다.

코드 채점 결과

+ Recent posts