내가 작성한 코드
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을 이용하여 명령어를 판단하고 조건에 따라 나누어 작성하였다.
코드 채점 결과
'BAEKJOON' 카테고리의 다른 글
[BaekJoon]11053번 - 가장 긴 증가하는 부분 수열(DP) (0) | 2019.08.04 |
---|---|
[BaekJoon]9012번 - 괄호 (0) | 2019.08.03 |
[BaekJoon]11004번 - K번째 수(정렬) (0) | 2019.08.02 |
[BaekJoon]2156번 - 포도주 시식(점화식 dp) (0) | 2019.08.02 |
[BaekJoon]11652번 - 카드 (0) | 2019.08.01 |