-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10828.txt
73 lines (65 loc) · 1.29 KB
/
10828.txt
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
import java.util.*;
import java.io.*;
public class Main
{
static ArrayList<Integer> arr = new ArrayList<Integer>();
static StringBuilder sb = new StringBuilder();
static void push(int num)
{
arr.add(num);
}
static void pop()
{
if (arr.size() > 0)
{
sb.append(arr.get(arr.size() - 1)+"\n");
arr.remove(arr.size() - 1);
} else
{
sb.append(-1+"\n");
}
}
static void size()
{
sb.append(arr.size() +"\n");
}
static void empty()
{
if (arr.size() > 0)
{
sb.append(0 + "\n");
} else
sb.append(1 + "\n");
}
static void top()
{
if (arr.size() == 0)
sb.append(-1 +"\n");
else
sb.append(arr.get(arr.size() - 1) + "\n");
}
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(in.readLine());
for (int i = 0; i < num; i++)
{
StringTokenizer tt = new StringTokenizer(in.readLine());
String t = tt.nextToken();
int n = 0;
if (tt.countTokens() >= 1)
n = Integer.parseInt(tt.nextToken());
if (t.equals("push"))
push(n);
else if (t.equals("top"))
top();
else if (t.equals("size"))
size();
else if (t.equals("empty"))
empty();
else if(t.equals("pop"))
pop();
}
System.out.println(sb);
}
}