티스토리 뷰

Algorithm

[백준] S4 10816 숫자 카드 2 (java)

코딩브론즈 2021. 1. 1. 23:58

www.acmicpc.net/problem/10816

 

10816번: 숫자 카드 2

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,

www.acmicpc.net

 

풀이

 

1) 카드가 들어올때마다 맵에 추가

2) 맵(키) == null 이면 0 아니면 value를 append

3) 출력

 

주의사항

 

1) 없다

 

package com.baekJoon;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class BJ_S4_10816_숫자카드2 {
	static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
	static StringTokenizer tokens;
	static int N,M;
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		input = new BufferedReader(new StringReader(src));
		N = Integer.parseInt(input.readLine());
		Map<Integer, Integer> map = new HashMap<>();
		tokens = new StringTokenizer(input.readLine());
		for(int n=0; n<N; n++) {
			int num = Integer.parseInt(tokens.nextToken());
			if(map.get(num) == null) {
				map.put(num, 1);
			}else {
				map.replace(num, map.get(num)+1);
			}
		}
		M = Integer.parseInt(input.readLine());
		tokens = new StringTokenizer(input.readLine());
		for(int m=0; m<M; m++) {
			int num = Integer.parseInt(tokens.nextToken());
			if(map.get(num) == null) {
				output.append(0+" ");
			}else {
				output.append(map.get(num)+" ");
			}
		}
		output.close();
		
	}
	
	static String src =
			"10\r\n" + 
			"6 3 2 10 10 10 -10 -10 7 3\r\n" + 
			"8\r\n" + 
			"10 9 -5 2 3 4 5 -10";
}

 

 

후기

 

숫자카드 1이랑 전혀 다른 유형이다. 맵으로 하면 정말 쉽게 풀이가 가능하다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
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
글 보관함