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

[ BOJ/C# ] 11723 집합

by 왹져박사 2023. 9. 18.
728x90

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

 

11723번: 집합

첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.

www.acmicpc.net

switch문을 사용하면 깔끔하게 보일 듯했다.

using System;
using System.Text;
using System.IO;

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

            int m = int.Parse(sr.ReadLine());
            string[] input;
            List<int> list = new List<int>();
            int x = 0;
            for (int i = 0; i < m; i++)
            {
                input = sr.ReadLine().Split(' ');
                if (input[0] =="all")
                {
                    list.Clear();
                    for (int j = 1; j <= 20; j++) list.Add(j);
                    continue;
                }
                else if (input[0] == "empty")
                {
                    list.Clear();
                    continue;
                }
                else x = int.Parse(input[1]);

                switch (input[0])
                {
                    case "add":
                        if (!list.Contains(x)) list.Add(x);
                        break;
                    case "remove":
                        if (list.Contains(x)) list.Remove(x);
                        break;
                    case "check":
                        if (list.Contains(x)) sb.Append("1" + '\n');
                        else sb.Append("0" + '\n');
                        break;
                    case "toggle":
                        if (list.Contains(x)) list.Remove(x);
                        else list.Add(x);
                        break;
                }
            }
            sw.Write(sb);
            sr.Close();
            sw.Flush();
            sw.Close();
        }
    }
}

728x90

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

[ BOJ/C# ] 9095 1, 2, 3 더하기  (0) 2023.09.19
[ BOJ/C# ] 1463 1로 만들기  (0) 2023.09.18
[ BOJ/C# ] 17219 비밀번호 찾기  (0) 2023.09.16
[ BOJ/C# ] 11399 ATM  (0) 2023.09.16
[ BOJ/C# ] 11047 동전 0  (0) 2023.09.15