Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22.02.07 - [BOJ] 11723. 집합 #84

Closed
suhyunsim opened this issue Jun 12, 2021 · 0 comments
Closed

22.02.07 - [BOJ] 11723. 집합 #84

suhyunsim opened this issue Jun 12, 2021 · 0 comments
Assignees
Labels
비트마스킹 실버 BOJ - 실버 실패 시도했지만 맞지 못한 문제

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Jun 12, 2021

문제

핵심 아이디어

  • '-1' 안해줘서 처음에 틀림
  • 비트연산의 개념을 이해해야 풀 수 있을듯

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q11723;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int m = Integer.parseInt(br.readLine());
        int n = 20;
        int s = 0;
        StringBuilder sb = new StringBuilder();
        while (m-- > 0) {
            String[] line = br.readLine().split(" ");
            switch (line[0]) {
                //add x: S에 x를 추가한다. (1 ≤ x ≤ 20) S에 x가 이미 있는 경우에는 연산을 무시한다.
                case "add": {
                    int x = Integer.parseInt(line[1]) - 1;
                    s = (s | 1 << x);
                    break;
                }
                //remove x: S에서 x를 제거한다. (1 ≤ x ≤ 20) S에 x가 없는 경우에는 연산을 무시한다.
                case "remove": {
                    int x = Integer.parseInt(line[1]) - 1;
                    s = (s & ~(1 << x));
                    break;
                }
                //check x: S에 x가 있으면 1을, 없으면 0을 출력한다. (1 ≤ x ≤ 20)
                case "check": {
                    int x = Integer.parseInt(line[1]) - 1;
                    int result = (s & (1 << x));
                    if (result == 0) {
                        sb.append("0\n");
                    } else {
                        sb.append("1\n");
                    }
                    break;
                }
                //toggle x: S에 x가 있으면 x를 제거하고, 없으면 x를 추가한다. (1 ≤ x ≤ 20)
                case "toggle": {
                    int x = Integer.parseInt(line[1]) - 1;
                    s = (s ^ (1 << x));
                    break;
                }
                //all: S를 {1, 2, ..., 20} 으로 바꾼다.
                case "all":
                    s = (1 << n) - 1;
                    break;
                //empty: S를 공집합으로 바꾼다. 
                case "empty":
                    s = 0;
                    break;
            }
        }
        System.out.println(sb);
    }
}
@suhyunsim suhyunsim added the 실버 BOJ - 실버 label Jun 12, 2021
@suhyunsim suhyunsim added this to the 6월 2주 차 milestone Jun 12, 2021
@suhyunsim suhyunsim self-assigned this Jun 12, 2021
@suhyunsim suhyunsim removed this from the 6월 2주 차 milestone Feb 5, 2022
@suhyunsim suhyunsim changed the title 21.06.11 - [BOJ] 11723. 집합 22.02.07 - [BOJ] 11723. 집합 Feb 7, 2022
@suhyunsim suhyunsim added the 실패 시도했지만 맞지 못한 문제 label Feb 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
비트마스킹 실버 BOJ - 실버 실패 시도했지만 맞지 못한 문제
Projects
None yet
Development

No branches or pull requests

1 participant