-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10828
More file actions
85 lines (81 loc) · 1.17 KB
/
10828
File metadata and controls
85 lines (81 loc) · 1.17 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
int n, num, cnt = 0;
char command[7];
int stack[10000] = { 0, };
void push(int i) {
scanf("%d", &num);
for (int j = 0; j <= i; j++) {
if (stack[j] == 0) {
stack[j] = num;
break;
}
}
}
void pop(int i) {
for (int j = i; j >= 0; j--) {
if (stack[j] != 0) {
printf("%d\n", stack[j]);
stack[j] = 0;
cnt = 1;
break;
}
}
if (cnt == 0) {
printf("-1\n");
}
cnt = 0;
}
void top(int i) {
for (int j = i; j >= 0; j--) {
if (stack[j] != 0) {
printf("%d\n", stack[j]);
cnt = 1;
break;
}
}
if (cnt == 0) {
printf("-1\n");
}
cnt = 0;
}
int size(int i) {
for (int j = 0; j <= i; j++) {
if (stack[j] != 0) {
cnt++;
}
}
printf("%d\n", cnt);
return cnt;
cnt = 0;
}
void empty(int i){
if (size(i) == 0) {
printf("1\n");
}
else {
printf("0\n");
}
cnt = 0;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", &command);
if (command[0] == 'p' && command[1] == 'u') {
push(i);
}
else if (command[0] == 'p') {
pop(i);
}
else if (command[0] == 't') {
top(i);
//pop
}
else if (command[0] == 's') {
size(i);
}
else if (command[0] == 'e') {
empty(i);
}
}
}