본문 바로가기
온라인 저지/BOJ

[BOJ/Java] 11723 집합

by ahj 2022. 2. 15.
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));
		StringBuilder sb = new StringBuilder();
		int n = Integer.parseInt(br.readLine());
		StringTokenizer st;

		int set = 0;
		for (int i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine());
			int cmdNum = 0;
			String cmd = st.nextToken();
			if (st.hasMoreTokens()) {
				cmdNum = Integer.parseInt(st.nextToken());
			}
			switch (cmd) {
			case "add":
				set = set | 1 << cmdNum;
				break;
			case "remove":
				if ((set & 1 << cmdNum) != 0) {
					set = set ^ 1 << cmdNum;
				}
				break;
			case "check":
				if ((set & 1 << cmdNum) != 0) {
					sb.append("1\n");
				} else {
					sb.append("0\n");
				}
				break;
			case "toggle":
				if ((set & 1 << cmdNum) != 0) {
					set = set ^ 1 << cmdNum;
				} else {
					set = set | 1 << cmdNum;
				}
				break;
			case "all":
				set = (int) Math.pow(2, 21) - 1;
				break;
			case "empty":
				set = 0;
				break;
			}
		}
		System.out.println(sb);
	}
}

'온라인 저지 > BOJ' 카테고리의 다른 글

[BOJ/Java] 14889 스타트와 링크  (0) 2022.02.17
[BOJ/Java] 1182 부분수열의 합  (0) 2022.02.16
[BOJ/Java] 2839 설탕 배달  (0) 2022.02.15
[BOJ/Java] 1074 Z  (0) 2022.02.15
[BOJ/Java] 3040 백설 공주와 일곱 난쟁이  (0) 2022.02.14

댓글