728x90
๐๋ฌธ์ ๋งํฌ
๐ป์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int bitmask = 0;
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
String operation = st.nextToken();
if (operation.equals("add")) {
int x = Integer.parseInt(st.nextToken()) - 1;
bitmask |= 1 << x;
} else if (operation.equals("remove")) {
int x = Integer.parseInt(st.nextToken()) - 1;
bitmask &= ~(1 << x);
} else if (operation.equals("check")) {
int x = Integer.parseInt(st.nextToken()) - 1;
sb.append((bitmask & (1 << x)) != 0 ? 1 : 0).append("\n");
//System.out.println((bitmask & (1 << x)) == 0 ? "0" : "1");
} else if (operation.equals("toggle")) {
int x = Integer.parseInt(st.nextToken()) - 1;
bitmask ^= (1 << x);
} else if (operation.equals("all")) {
bitmask = (1 << 21) - 1;
} else if (operation.equals("empty")) {
bitmask = 0;
}
}
System.out.println(sb);
}
}
โณํ๊ณ
- ์ฒ์์ ๋ฌธ์ ๋ฅผ ์ฝ๊ณ HashSet์ ์ฌ์ฉํด์ ํ์ดํ๋๋ฐ ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋ค. ๋ค์์ boolean๋ฐฐ์ด์ ์ฌ์ฉํด์ ํ์ด๋ฅผ ํ๋๋ฐ ์ด ๋ํ ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋ค.
- ๋นํธ๋ง์คํน์ผ๋ก ํ์ด๋ฅผ ํ์์๋ ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋๋ฐ, ํน์๋ ์ถ์ด์ ์ถ๋ ฅ ๋ฐฉ์์ StringBuilder๋ก ์์ ํ๋๋ ํต๊ณผ๋ฅผํ๋ค.
728x90
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm/Baekjoon] ๊ฒน์น๋ ๊ฑด ์ซ์ด - 20922 (S1/JAVA) (0) | 2023.06.21 |
---|---|
[Algorithm/Baekjoon] ๋ ๊ฒ์ - 9655 (S5/JAVA) (1) | 2023.06.19 |
[Algorithm/Baekjoon] 1์ ๊ฐ์ ์ธ๊ธฐ - 9527 (G2/JAVA) (0) | 2023.06.14 |
[Algorithm/Baekjoon] ์ปจ๋ฒ ์ด์ด ๋ฒจํธ ์์ ๋ก๋ด - 20055 (G5/JAVA) (0) | 2023.06.13 |
[Algorithm/Baekjoon] ์ฌ์ด ์ต๋จ๊ฑฐ๋ฆฌ - 14940 (S1/JAVA) (0) | 2023.06.10 |