[BOJ][10816] 숫자 카드 2
Updated:
1. 문제 링크
https://www.acmicpc.net/problem/10816
2. 사용 알고리즘
구현
3. 풀이
배열에 카드 별 개수를 저장한 후 저장된 개수 출력
4. 소스 코드
4-1. C++
https://github.com/dev-aiden/problem-solving/blob/main/boj/10816.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <algorithm>
using namespace std;
int n, m, idx;
int arr[500003], arr2[500003];
int cnt[20000003];
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n;
for(int i = 0; i < n; ++i) {
int num; cin >> num;
++cnt[num + 10000000];
}
cin >> m;
for(int i = 0; i < m; ++i) cin >> arr2[i];
for(int i = 0; i < m; ++i) cout << cnt[arr2[i] + 10000000] << " ";
cout << "\n";
return 0;
}
4-2. JAVA
https://github.com/dev-aiden/problem-solving/blob/main/boj/10816.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static int n, m, idx;
static int[] arr = new int[500003];
static int[] arr2 = new int[500003];
static int[] cnt = new int[20000003];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i = 0; i < n; ++i) {
int num = Integer.parseInt(st.nextToken());
++cnt[num + 10000000];
}
m = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
for(int i = 0; i < m; ++i) arr2[i] = Integer.parseInt(st.nextToken());
StringBuilder sb = new StringBuilder();
for(int i = 0; i < m; ++i) sb.append(cnt[arr2[i] + 10000000]).append(" ");
sb.append("\n");
System.out.println(sb);
}
}
Leave a comment