이 문제에서 연습할 수 있다.

아래 소스는 위 문제의 답안이기도 하다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;
int main() {
    int S = 0;
    int M; scanf("%d"&M); while (M--) {
        char a[10]; int b; scanf("%s", a);
 
        string s = a;
        if (s == "add"scanf("%d"&b), S |= (1 << (b - 1));
        if (s == "check"scanf("%d"&b), printf("%d\n", S & (1 << (b - 1)) ? 1 : 0);
        if (s == "remove"scanf("%d"&b), S &= ~(1 << (b - 1));
        if (s == "toggle"scanf("%d"&b), S ^= (1 << (b - 1));
        if (s == "all") S = 0 - 1;
        if (s == "empty") S = 0;
    }
    return 0;
}
cs

조금 까리하건 remove와 toggle인데 생각해내기 어렵진 않다.



반응형

+ Recent posts