☕Language: Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
Set<Integer> set1 = new TreeSet<>();
StringTokenizer stN = new StringTokenizer(br.readLine());
while (N-- > 0) {
set1.add(Integer.parseInt(stN.nextToken()));
}
Set<Integer> set2 = new TreeSet<>();
StringTokenizer stM = new StringTokenizer(br.readLine());
while (M-- > 0) {
set2.add(Integer.parseInt(stM.nextToken()));
}
int A = return_size(set1, set2);
int B = return_size(set2, set1);
bw.write((A + B) + "");
bw.flush();
bw.close();
}
public static int return_size(Set<Integer> set1, Set<Integer> set2) {
int cnt = 0;
for (int i : set2) {
if (!set1.contains(i)) {
cnt++;
}
}
return cnt;
}
}
|
🤔 해설
1. set1.add(Integer.parseInt(stN.nextToken()));
- 첫 번째 입력값을 set1에 저장
2. set2.add(Integer.parseInt(stM.nextToken()));
- 두 번째 입력값을 set2에 저장
3. if (!set1.contains(i)) { cnt++; }
- set2 요소 값이 set1에 있을 경우, cnt 증가 후 return
😮 이 외의 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class Main {
static StringTokenizer st;
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
Set<Integer> set = new HashSet<>();
for (int i = 0; i < N; i++) {
set.add(Integer.valueOf(st.nextToken()));
}
st = new StringTokenizer(br.readLine());
int cnt = 0;
for (int i = 0; i < M; i++) {
int num = Integer.valueOf(st.nextToken());
cnt = set.contains(num) ? cnt + 1 : cnt;
set.add(num);
}
bw.write((set.size() - cnt) + "");
bw.flush();
bw.close();
}
}
|
1. static StringTokenizer st;
- StringTokenizer 전역 변수 선언
2. cnt = set.contains(num) ? cnt + 1 : cnt;
- cnt: 입력값1과 입력값2의 공통 요소 개수
3. bw.write((set.size() - cnt) + "");
- 중복이 제거된 set에서 중복된 값인 cnt 차감
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class Main {
static StringTokenizer st;
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
Set<Integer> setN = new HashSet<>();
Set<Integer> setM = new HashSet<>();
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
setN.add(Integer.parseInt(st.nextToken()));
}
st = new StringTokenizer(br.readLine());
for (int i = 0; i < M; i++) {
setM.add(Integer.parseInt(st.nextToken()));
}
int cnt = 0;
for (int i : setN) {
cnt = !setM.contains(i) ? cnt + 1 : cnt;
}
for (int i : setM) {
cnt = !setN.contains(i) ? cnt + 1 : cnt;
}
bw.write(cnt + "");
bw.flush();
bw.close();
}
}
|
🔗 소스 코드
GitHub
📚 참고 자료
'Computer > Algorithm_Java' 카테고리의 다른 글
[BaekJoon] 27423번 녹색거탑 문제 풀이 (Success) (0) | 2023.09.07 |
---|---|
[BaekJoon] 15439번 베라의 패션 문제 풀이 (Success) (0) | 2023.09.06 |
[BaekJoon] 1764번 듣보잡 문제 풀이 (Success) (0) | 2023.09.04 |
[BaekJoon] 2164번 카드2 문제 풀이 (Success) (0) | 2023.09.03 |
[BaekJoon] 28279번 덱2 문제 풀이 (Success) (0) | 2023.09.01 |