Fork me on GitHub

牛客网编程练习之编程马拉松:寻找舞伴

image

image

 

标记一下即可,只是记得需要区分男生和女生,虽然同性才是真爱...

 

AC代码:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author CC11001100
 */
public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		while (sc.hasNextInt()) {
			int ans = 0;
			int n = sc.nextInt();
			int m = sc.nextInt();
			Map<Integer, Integer> count = new HashMap<>();
			while (n-- > 0) {
				int t = sc.nextInt();
				count.put(t, count.getOrDefault(t, 0) + 1);
			}
			while (m-- > 0) {
				int t = sc.nextInt();
				int k = count.getOrDefault(t, 0);
				if (k > 0) {
					ans++;
					count.put(t, k - 1);
				}
			}
			System.out.println(ans);
		}

	}

}

 

题目来源: https://www.nowcoder.com/practice/33b1a1a460914456ae2b3afb16f095af?tpId=3&tqId=10902&tPage=1&rp=&ru=/ta/hackathon&qru=/ta/hackathon/question-ranking

 

.

posted @ 2017-12-13 01:34  CC11001100  阅读(218)  评论(0编辑  收藏  举报