File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # [ Silver V] 집합 - 11723
2+
3+ [ 문제 링크] ( https://www.acmicpc.net/problem/11723 )
4+
5+ ### 성능 요약
6+
7+ 메모리: 314352 KB, 시간: 1484 ms
8+
9+ ### 분류
10+
11+ 비트마스킹, 구현
12+
13+ ### 제출 일자
14+
15+ 2025년 3월 16일 17:43:50
16+
17+ ### 문제 설명
18+
19+ <p >비어있는 공집합 S가 주어졌을 때, 아래 연산을 수행하는 프로그램을 작성하시오.</p >
20+
21+ <ul >
22+ <li><code>add x</code>: S에 x를 추가한다. (1 ≤ x ≤ 20) S에 x가 이미 있는 경우에는 연산을 무시한다.</li>
23+ <li><code>remove x</code>: S에서 x를 제거한다. (1 ≤ x ≤ 20) S에 x가 없는 경우에는 연산을 무시한다.</li>
24+ <li><code>check x</code>: S에 x가 있으면 1을, 없으면 0을 출력한다. (1 ≤ x ≤ 20)</li>
25+ <li><code>toggle x</code>: S에 x가 있으면 x를 제거하고, 없으면 x를 추가한다. (1 ≤ x ≤ 20)</li>
26+ <li><code>all</code>: S를 {1, 2, ..., 20} 으로 바꾼다.</li>
27+ <li><code>empty</code>: S를 공집합으로 바꾼다.</li>
28+ </ul >
29+
30+ ### 입력
31+
32+ <p >첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다.</p >
33+
34+ <p >둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.</p >
35+
36+ ### 출력
37+
38+ <p ><code >check</code > 연산이 주어질때마다, 결과를 출력한다.</p >
39+
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ import java .io .*;
3+
4+
5+ // Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
6+ // then press Enter. You can now see whitespace characters in your code.
7+ public class Main {
8+ static Set <Integer > set = new HashSet <>();
9+ static StringBuilder sb = new StringBuilder ();
10+
11+ public static void main (String [] args ) throws IOException {
12+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
13+ int M = Integer .parseInt (br .readLine ());
14+
15+ for (int i =0 ; i <M ; i ++){
16+ StringTokenizer st = new StringTokenizer (br .readLine ());
17+
18+ String calc_type = st .nextToken ();
19+
20+ int calc_cnt = 0 ; // 기본값 설정
21+ if (st .hasMoreTokens ()) { // 토큰이 존재하는 경우에만 숫자 읽기
22+ calc_cnt = Integer .parseInt (st .nextToken ());
23+ }
24+
25+ switch (calc_type ){
26+ case "add" :
27+ add (calc_cnt );
28+ break ;
29+ case "remove" :
30+ remove (calc_cnt );
31+ break ;
32+ case "check" :
33+ check (calc_cnt );
34+ break ;
35+ case "toggle" :
36+ toggle (calc_cnt );
37+ break ;
38+ case "all" :
39+ all ();
40+ break ;
41+ case "empty" :
42+ empty ();
43+ break ;
44+ }
45+ }
46+
47+ System .out .println (sb );
48+
49+ }
50+
51+ public static void add (int x ){
52+ if (!set .contains (x )) {
53+ set .add (x );
54+ }
55+ }
56+
57+ public static void remove (int x ){
58+ if (set .contains (x )){
59+ set .remove (x );
60+ }
61+ }
62+
63+ public static void check (int x ){
64+ if (set .contains (x )){
65+ sb .append (1 ).append ("\n " );
66+ }else {
67+ sb .append (0 ).append ("\n " );
68+ }
69+ }
70+
71+ public static void toggle (int x ){
72+ if (set .contains (x )){
73+ set .remove (x );
74+ }else {
75+ set .add (x );
76+ }
77+ }
78+
79+ public static void all (){
80+ set .clear ();
81+ for (int i =1 ; i <=20 ; i ++){
82+ set .add (i );
83+ }
84+ }
85+
86+ public static void empty (){
87+ set .clear ();
88+ }
89+ }
You can’t perform that action at this time.
0 commit comments