본문 바로가기
알고리즘/백준 BOJ

[ BOJ/C# ] 10845 큐

by 왹져박사 2023. 8. 22.
728x90

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

 

10845번: 큐

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

https://learn.microsoft.com/ko-kr/dotnet/api/system.collections.generic.queue-1?view=net-7.0 

 

Queue<T> 클래스 (System.Collections.Generic)

개체의 선입선출(FIFO) 컬렉션을 나타냅니다.

learn.microsoft.com

Queue.Enqueue(n);  //큐의 끝에 개체를 추가함

Queue.Dequeue();  //큐의 가장 앞 개체를 제거하고 반환

Queue.Peek();  //큐의 가장 앞 개체를 제거하지 않고 반환

Queue.Count();  //큐의 모든 요소 수

Queue.Last();  //큐의 가장 마지막 요소 반환

using System;

namespace _10845
{
    class Program
    {
        static void Main()
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            int n = int.Parse(sr.ReadLine());  //명령 수
            Queue<int> q = new Queue<int>();

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


                if (inputs[0] == "push") q.Enqueue(int.Parse(inputs[1]));
                else if (inputs[0] == "size") sw.WriteLine(q.Count());
                else if (inputs[0] == "empty")
                {
                    if (q.Count == 0) sw.WriteLine("1");
                    else sw.WriteLine("0");
                }
                else
                {
                    if (q.Count == 0) sw.WriteLine("-1");
                    else
                    {
                        if (inputs[0] == "pop") sw.WriteLine(q.Dequeue());
                        else if (inputs[0] == "front")
                        {
                            int f = q.Peek();
                            if (f == null) sw.WriteLine("-1");
                            else sw.WriteLine(f);
                        }
                        else if (inputs[0] == "back") sw.WriteLine(q.Last());
                    }
                }
            }
            sr.Close();
            sw.Flush();
            sw.Close();
        }
    }
}

728x90

'알고리즘 > 백준 BOJ' 카테고리의 다른 글

[ BOJ/C# ] 2751 수 정렬하기 2  (0) 2023.08.23
[ BOJ/C# ] 10828 스택  (0) 2023.08.23
[ BOJ/C# ] 2675 문자열 반복  (0) 2023.08.21
[ BOJ/C# ] 2920 음계  (0) 2023.08.21
[ BOJ/C# ] 3052 나머지  (0) 2023.08.21