직접 만들어보니까 재밌네. 이해도 잘되고.


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
using System;
using static System.Runtime.InteropServices.JavaScript.JSType;
 
class Program
{
    static void Main(string[] args)
    {
 
        using StreamReader sr = new StreamReader(new BufferedStream(Console.OpenStandardInput()));
        using StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()));
 
        int= int.Parse(sr.ReadLine());
        int cntV = -1;
 
        int[] stack_V = new int[N];
 
    
        for(int= 0; i < N; i++)
        {
            string[] Command = sr.ReadLine().Split();
            //push X: 정수 X를 스택에 넣는 연산이다.
            if (Command[0== "push")
            {
                int num = int.Parse(Command[1]);
                stack_V[cntV] = num;
                cntV++;
            }
            //pop: 스택에서 가장 위에 있는 정수를 빼고, 그 수를 출력한다.
            //만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.
            if (Command[0== "pop")
            {
                if (cntV == 0)
                {
                    sw.WriteLine("-1");
                }
                else
                {
                    cntV--;
                    sw.WriteLine(stack_V[cntV]);
                    stack_V[cntV] = 0;
                    
                }
            }
            //size: 스택에 들어있는 정수의 개수를 출력한다.
            if (Command[0== "size")
            {
                sw.WriteLine(cntV);
            }
            //empty: 스택이 비어있으면 1, 아니면 0을 출력한다.
 
            if (Command[0== "empty")
            {
                if (cntV == 0)
                {
                    sw.WriteLine("1");
                }
                else
                {
                    sw.WriteLine("0");
                }
            }
            //top: 스택의 가장 위에 있는 정수를 출력한다.
            //만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.
            if (Command[0== "top")
            {
                if (cntV == 0)
                {
                    sw.WriteLine("-1");
                }
                else
                {
                    sw.WriteLine(stack_V[cntV - 1]);
                }
            }
        }
    }
}
 
cs