https://www.acmicpc.net/problem/10845

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Exam01_05
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            int N = int.Parse(sr.ReadLine());
            Queue<int> que = new Queue<int>();
            List<int> ansList = new List<int>();

            for (int i = 0; i < N; i++)
            {
                string[] str = sr.ReadLine().Split(' ');

                if (str[0] == "push")
                {
                    que.Enqueue(int.Parse(str[1]));
                }
                else if (str[0] == "pop")
                {
                    if (que.Count > 0) ansList.Add(que.Dequeue());
                    else ansList.Add(-1);
                }
                else if (str[0] == "size")
                {
                    ansList.Add(que.Count);
                }
                else if (str[0] == "empty")
                {
                    if (que.Count > 0) ansList.Add(0);
                    else ansList.Add(1);
                }
                else if (str[0] == "front")
                {
                    if (que.Count > 0) ansList.Add(que.First());
                    else ansList.Add(-1);
                }
                else if (str[0] == "back")
                {
                    if (que.Count > 0) ansList.Add(que.Last());
                    else ansList.Add(-1);
                }
            }

            foreach (int ans in ansList)
            {
                sw.WriteLine(ans);
            }

            sr.Close();
            sw.Close();
        }
    }
}

 

'Algorithm > BOJ' 카테고리의 다른 글

C#) [BOJ] 4949 균형잡힌 세상  (0) 2023.01.26
C#) [BOJ] 10828 스택  (0) 2023.01.26
C#) [BOJ] 2591 숫자 카드 2  (0) 2023.01.25
C#) [BOJ] 11650 좌표 정렬하기  (0) 2023.01.25
C#) [BOJ] 2839 설탕 배달 ★  (0) 2023.01.25

+ Recent posts