-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo_10828.java
More file actions
75 lines (63 loc) · 1.79 KB
/
No_10828.java
File metadata and controls
75 lines (63 loc) · 1.79 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
/*
Problem_10828_스택
https://www.acmicpc.net/problem/10828
자료구조 : Stack
*/
import java.io.*;
public class No_10828 {
static int top = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int testCommand = Integer.parseInt(br.readLine());//명령어 개수
int[] stack = new int[testCommand];
for(int repeat = 0; repeat < testCommand; repeat++) {
String command = br.readLine();
switch (command) {
case "top":
bw.write(top(stack) + "\n");
break;
case "pop":
bw.write(pop(stack) + "\n");
break;
case "size":
bw.write(size() + "\n");
break;
case "empty":
bw.write(empty() + "\n");
break;
default:
push(stack, Integer.parseInt(command.substring(5)));
}
}
bw.flush(); bw.close(); br.close();
}
private static void push(int[] stack, int value) {
stack[++top] = value;
}
private static int size() {
return top;
}
private static int empty() {
if (top == 0) {
return 1;
}
else {
return 0;
}
}
private static int top(int[] stack) {
if(top != 0) {
return stack[top];
}
else {
return -1;
}
}
private static int pop(int[] stack) {
if (top == 0) {
return -1;
}
return stack[top--];
}
}