문제 출처: https://www.acmicpc.net/problem/10828



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
86
87
88
89
90
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class algo3_10828_stack {
    
    private static BufferedReader reader;
    private static StringTokenizer tokenizer;
    private static StringBuilder strBuilder;
    
    private static int N, stackSize = 0;
    private static int[] stackArr;
    
    public static void main(String[] args) throws NumberFormatException, IOException {
 
        reader = new BufferedReader(new InputStreamReader(System.in));
        strBuilder = new StringBuilder();
        
        N = Integer.parseInt(reader.readLine());
        stackArr = new int[N];
        
        for(int i=0 ; i<N ; i++){
            stack(reader.readLine());
        }
        System.out.println(strBuilder);
    }
    
    public static void stack(String command){
        tokenizer = new StringTokenizer(command);
        
        switch(tokenizer.nextToken()){
            case "push":
                push(Integer.parseInt(tokenizer.nextToken()));
                break;
            case "pop":
                strBuilder.append(pop() + "\n");
                break;
            case "size":
                strBuilder.append(size() + "\n");
                break;
            case "empty":
                if(empty())
                    strBuilder.append("1\n");
                else
                    strBuilder.append("0\n");
                break;
            case "top":
                strBuilder.append(top() + "\n");
                break;
            default:
                break;
        }
    }
 
    public static void push(int num){
        stackArr[++stackSize] = num;
    }
 
    public static int pop(){
        int num = -1;
        if(stackSize == 0)
            return num;
        else{
            num = stackArr[stackSize];
            stackArr[stackSize--= 0;
            return num;
        }
    }
 
    public static int size(){
        return stackSize;
    }
 
    public static boolean empty(){
        if(stackSize == 0)
            return true;
        else
            return false;
    }
 
    public static int top(){
        if(stackSize == 0)
            return -1;
        else
            return stackArr[stackSize];
    }
}
 
cs




'Algorithm' 카테고리의 다른 글

백준 10799번: 쇠막대기  (0) 2018.07.20
백준 9012번: 괄호  (0) 2018.07.18
백준 1260번: DFS와 BFS  (0) 2018.04.18
BFS (Breadth-First Search)  (0) 2018.04.12
DFS (Depth-First Search)  (0) 2018.04.12

+ Recent posts