< [C++ / 백준 11723번] 집합

프로그래밍 공부/백준 (C++)

[C++ / 백준 11723번] 집합

Rocketbabydolls 2025. 2. 18. 17:45

 

 

 

시간복잡도에 유의하여 풀이해야 한다. 인덱스 배열을 만들어 해결했다.

 

#include <iostream>
using namespace std;

int M;
int S[22];

int main() {
    cin.tie(NULL); 
    ios::sync_with_stdio(false);
    
    cin >> M;

    string inputoper;
    int input;


    for (int i = 0; i < M; i++)
    {
        cin >> inputoper;

        if (inputoper == "add")
        {
            cin >> input;

            if (!S[input]) S[input] = 1;

        }
        else if (inputoper == "remove")
        {
            cin >> input;

            if (S[input])
            {
                S[input] = 0;
            }
        }
        else if (inputoper == "check")
        {
            cin >> input;
            
            cout << S[input];

            cout << '\n';

        }
        else if (inputoper == "toggle")
        {
            cin >> input;
            
            if (S[input])
            {
                S[input] = 0;
            }
            else
            {
                S[input] = 1;
            }
        }
        else if (inputoper == "all")
        {
            for (int i = 1; i < 21; i++)
            {
                S[i] = 1;
            }
        }
        else if (inputoper == "empty")
        {
            for (int i = 1; i < 21; i++)
            {
                S[i] = 0;
            }
        }

    }
    

    
    return 0;
}