ALGORITHM
백준 10816 숫자 카드 2 (Java)
공부하는_다온
2023. 4. 14. 22:00
1. 문제 링크
10816번: 숫자 카드 2
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,
www.acmicpc.net
2. 문제 및 입출력예제
3. 문제 풀이
map에 숫자 카드에 적힌 정수와 개수를 넣는다.
containsKey()를 이용해서 입력받은 수가 map에 들어있는지 확인하고 출력한다.
4. 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
String[] split = br.readLine().split(" ");
HashMap<Integer, Integer> map = new HashMap<>();
for(int i=0;i<N;i++) {
int num = Integer.parseInt(split[i]);
if(map.containsKey(num)) {
map.put(num, map.get(num)+1);
}
else {
map.put(num, 1);
}
}
int M = Integer.parseInt(br.readLine());
split = br.readLine().split(" ");
for(int i=0;i<M;i++) {
int num = Integer.parseInt(split[i]);
if(map.containsKey(num)) {
sb.append(map.get(num)+" ");
}
else {
sb.append("0 ");
}
}
System.out.println(sb);
}
}