백준 10828 스택

Console Programming/C# Console 2019. 11. 4. 10:23

Program.cs

using System;

namespace _10828
{
    class Program
    {

        static void Main(string[] args)
        {
            int[] stack = new int[1000];
            int cnt = 0;
            string[] keyword = new string[] { "push", "pop", "size", "empty", "top" };

            int testcase = int.Parse(Console.ReadLine());
            
            for (int i = 0; i < testcase; i++)
            {               
                string[] str2 = (Console.ReadLine()).Split(' ');
                for(int j =0; j < keyword.Length; j++)
                {
                    if(str2[0].Equals(keyword[j]))
                    {
                        switch (j)
                        {
                            case 0:
                                {
                                    int n = int.Parse(str2[1]);
                                    stack[cnt++] = n;
                                    break;

                                }
                            case 1:
                                {
                                    if (cnt == 0)
                                    {
                                        Console.WriteLine("-1");
                                        break;
                                    }
                                    else
                                    {
                                        Console.WriteLine(stack[--cnt]);                                        
                                    }
                                    break;
                                }
                            case 2:
                                {
                                    Console.WriteLine(cnt);
                                    break;
                                }
                            case 3:
                                {
                                    if (cnt == 0)
                                    {
                                        Console.WriteLine("1");
                                    }
                                    else
                                    {
                                        Console.WriteLine(0);
                                    }
                                    break;
                                }
                            case 4:
                                {
                                    if (cnt == 0)
                                    {
                                        Console.WriteLine("-1");
                                    }
                                    else
                                    {
                                        Console.WriteLine(stack[cnt - 1]);
                                    }
                                    break;
                                }
                        }
                    }        
                } 
            }
        }
    }
}

 

 

: