[BaekJoon] 3052번 - 나머지 [Java][C++]
[BaekJoon] 3052번 - 나머지 [Java][C++]
1. 문제 풀이
$42$ 로 나눈 겹치지 않는 나머지의 개수를 구하는 문제로 방문 체크 배열을 활용해서 해결할 수도 있고, 집합 자료구조를 활용해도 해결할 수 있다.
2. 코드
1. 방문 체크 배열 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
boolean[] visited = new boolean[42];
for (int i = 0; i < 10; i++) {
int num = Integer.parseInt(br.readLine());
visited[num % 42] = true;
}
int cnt = 0;
for (boolean flag : visited) {
if (flag) cnt++;
}
System.out.println(cnt);
}
}
2. 집합 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Set<Integer> set = new HashSet<>();
for (int i = 0; i < 10; i++) {
int num = Integer.parseInt(br.readLine());
set.add(num % 42);
}
System.out.println(set.size());
}
}
3. 방문 체크 배열 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<bool> visited(42);
for (int i = 0; i < 10; i++) {
int n;
cin >> n;
visited[n % 42] = true;
}
cout << count(visited.begin(), visited.end(), true);
}
4. 집합 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
unordered_set<int> st;
for (int i = 0; i < 10; i++) {
int n;
cin >> n;
st.insert(n % 42);
}
cout << st.size();
}
This post is licensed under CC BY 4.0 by the author.