https://www.acmicpc.net/problem/11723
11723번: 집합
첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.
www.acmicpc.net
이번 문제는 집합이다.
수학의 집합을 떠올리면 쉬운데,
어떤 집합에 우리가 원소를 추가하고 제거하는 작업이라고 생각하면 쉽다.
다만 집합이라고 하는 개념을 잠깐 살펴볼 필요가 있긴 한데,
(곧 작성할 예정이다...)
STL의 구현 형태의 문제이다.
6개의 집합 사용 방법이었는데, 진짜 별로 설명할 부분이 없다.
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
Set s=new TreeSet<Integer>();
Set all=new TreeSet(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)) ;
int M=Integer.parseInt(br.readLine());
for(int i=0;i<M;i++) {
String[] input=br.readLine().split(" ");
int x=0;
switch(input[0]) {
case "add":
x=Integer.parseInt(input[1]);
s.add(x);
break;
case "remove":
x=Integer.parseInt(input[1]);
s.remove(x);
break;
case "check":
x=Integer.parseInt(input[1]);
if(s.contains(x)) {
bw.write("1\n");
}else {
bw.write("0\n");
}
break;
case "toggle":
x=Integer.parseInt(input[1]);
if(s.contains(x))
s.remove(x);
else
s.add(x);
break;
case "all":
s.removeAll(s);
s.addAll(all);
break;
case "empty":
s.removeAll(all);
break;
}
}
bw.flush();
}
}
반응형
'Algorithm > Baekjoon For.Java' 카테고리의 다른 글
2775 : 부녀회장이 될테야 (0) | 2023.03.30 |
---|---|
1620 : 나는야 포켓몬 마스터 이다솜 (0) | 2023.03.24 |
1676 : 팩토리얼 0의 갯수 (0) | 2023.03.22 |
2292 : 벌집 (0) | 2023.03.20 |
1354 : 무한 수열 2 (0) | 2023.03.20 |