ALGORITHM
[JAVA] 백준 1822번- 차집합
연듀
2022. 7. 6. 11:17
https://www.acmicpc.net/problem/1822
1822번: 차집합
첫째 줄에는 집합 A의 원소의 개수 n(A)와 집합 B의 원소의 개수 n(B)가 빈 칸을 사이에 두고 주어진다. (1 ≤ n(A), n(B) ≤ 500,000)이 주어진다. 둘째 줄에는 집합 A의 원소가, 셋째 줄에는 집합 B의 원소
www.acmicpc.net
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int[] arrA = new int[a];
int[] arrB = new int[b];
TreeSet<Integer> aSet = new TreeSet<>();
for(int i=0; i<a; i++){
aSet.add(sc.nextInt());
}
for(int i=0; i<b; i++){
int k = sc.nextInt();
if(aSet.contains(k)) aSet.remove(k);
}
System.out.println(aSet.size());
for(int x:aSet){
System.out.print(x+" ");
}
}
}
a집합을 입력받아 모두 TreeSet에 추가한다.
그 다음 b집합 원소들을 입력받아 TreeSet에 이미 있다면 제거한다.
반응형