알고리즘/수학문제

[백준] 11723번 집합 #Java

VIPeveloper 2022. 5. 18. 21:53
728x90
반응형

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int s = 0;
        StringBuilder sb = new StringBuilder();

        while (n-->0) {
            StringTokenizer st = new StringTokenizer(br.readLine()," ");
            String cmd = st.nextToken();
            int a;

            switch (cmd){
                case "add" :
                    a = Integer.parseInt(st.nextToken());
                    s |= (1<<a);
                    break;
                case "remove":
                    a = Integer.parseInt(st.nextToken());
                    s &= ~(1<<a);
                    break;
                case "check":
                    a = Integer.parseInt(st.nextToken());
                    int tmp = s & (1<<a);
                    sb.append((tmp==0) ? 0 : 1).append('\n');
                    break;
                case "toggle":
                    a = Integer.parseInt(st.nextToken());
                    s ^= (1<<a);
                    break;
                case "all":
                    s = (1<<21) - 1;
                    break;
                case "empty":
                    s = 0;
                    break;
            }
        }
        System.out.println(sb);
    }
}
728x90
반응형