알고리즘 풀이

[C#] 백준 11723번 문제풀이

bimtaeur30 2025. 12. 29. 09:11

문제번호: 11723

문제명: 집합

문제링크: https://www.acmicpc.net/problem/11723

문제내용과 예제 입/출력은 위 문제 링크에서 확인해 주시기 바랍니다.

 

이번 문제는 실버 5인 것에 비해 비교적 간단하게 풀이할 수 있었던 문제였습니다.

Hashset을 사용하여 중복을 무시하고 빠른 입/출력을 통해 시간초과 문제를 해결하였습니다.

다만, Switch문을 사용하였더니 코드의 길이가 좀 길어져 가독성문제가 있는 것 같았습니다. 이 부분은 해결방안을 마련해 보겠습니다. 감사합니다.

public static void Main()
{
    HashSet<int> S = new HashSet<int>();
    StreamReader sr = new StreamReader(Console.OpenStandardInput());
    StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

    int M = int.Parse(sr.ReadLine());
    for(int i = 0; i < M; i++)
    {
        string[] input = sr.ReadLine().Split();
        string cmd = input[0];
        if (input.Length > 1 )
        {
            int number = int.Parse(input[1]);
            switch (cmd)
            {
                case "add":
                    {
                        S.Add(number);
                        break;
                    }
                case "remove":
                    {
                        S.Remove(number);
                        break;
                    }
                case "check":
                    {
                        if (S.Contains(number)) sw.WriteLine(1);
                        else
                        {
                            sw.WriteLine(0);
                        }
                        break;
                    }
                case "toggle":
                    {
                        if (S.Contains(number)) S.Remove(number);
                        else
                        {
                            S.Add(number);
                        }
                        break;
                    }
            }
        }
        else
        {
            switch (cmd)
            {
                case "all":
                    {
                        S.Clear();
                        S = new HashSet<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
                        break;
                    }
                case "empty":
                    {
                        S.Clear();
                        break;
                    }
            }
        }
    }

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




'알고리즘 풀이' 카테고리의 다른 글

[C#] 백준 17219번 문제풀이  (0) 2025.12.31
[C#] 백준 11047번 문제풀이  (0) 2025.12.30
[C#] 백준 30802번 문제풀이  (0) 2025.12.29
[C#] 백준 2775번 문제풀이  (0) 2025.12.26
[C#] 백준 28702번 문제풀이  (0) 2025.10.19