하루 한 접시

[백준] 10828번: 스택 [C#]

NaZZU 2024. 5. 12. 01:31

 

 

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
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _10828_boj boj = new _10828_boj();
 
            boj.boj_10828();
 
        }
    }
        internal class _10828_boj
    {
        public void boj_10828()
        {
            StreamReader reader = new StreamReader(Console.OpenStandardInput());
            StringBuilder sb = new StringBuilder();
 
            int n = int.Parse(reader.ReadLine());
 
            List<string> stack = new List<string>();
            int top = -1;
 
            string[] input = new string[2];
 
            for (int i = 0; i < n; i++)
            {
                input = reader.ReadLine().Split();
 
                switch (input[0])
                {
                    case "push":
                        top += 1;
                        stack.Add(input[1]);
                        break;
                    case "pop":
                        if (top > -1)
                        {
                            sb.Append(stack[top]+"\n");
                            stack.RemoveAt(top);
                            top -= 1;
                        }
                        else
                            sb.Append("-1\n");
                        break;
                    case "size":
                        sb.Append((top + 1+ "\n");
                        break;
                    case "empty":
                        sb.Append(top > -1 ? "0\n" : "1\n");
                        break;
                    case "top":
                        sb.Append(top > -1 ? stack[top]+"\n" : "-1\n");
                        break;
 
                }
            }
            if (sb.Length > 0 && sb[sb.Length - 1== '\n')
                sb.Remove(sb.Length - 11);
            Console.WriteLine(sb);
        }
    }
}
 
 
cs

이 쉬운걸 풀겠다고 몇번을 틀린건지...