집합 11723

PublicMinsu·2023년 3월 19일
0

문제

접근 방법

bool 배열을 하나 두어서 해당하는 수가 존재하는지 확인할 수 있다. 각각에 해당하는 연산을 bool 배열을 활용하여서 해결하면 된다.

코드

#include <iostream>
#include <string>
using namespace std;
bool set[21];
int main()
{
    ios::sync_with_stdio(0), cin.tie(0);
    int M;
    string com;
    int val;
    cin >> M;
    while (M--)
    {
        cin >> com;
        if (com != "all" && com != "empty")
            cin >> val;
        if (com == "add")
            set[val] = true;
        else if (com == "remove")
            set[val] = false;
        else if (com == "check")
            cout << (int)set[val] << "\n";
        else if (com == "toggle")
            set[val] = !set[val];
        else if (com == "all")
            for (int i = 1; i <= 20; ++i)
                set[i] = true;
        else
            for (int i = 1; i <= 20; ++i)
                set[i] = false;
    }
    return 0;
}

풀이

bool을 활용할 수 있으면 해결할 수 있다.
태그를 보니 비트 마스킹이 적혀있었다.
비트 마스킹을 활용하면 더 빠른 시간을 보여줄 것이다. (가득 채우고 비우는 과정에서 반복문을 활용 안 해도 된다.)

profile
연락 : publicminsu@naver.com

0개의 댓글