【洛谷P1571】

题目描述
虽然 Miss Medusa 到了北京,领了科技创新奖,但是她还是觉得不满意。原因是:他发现很多人都和她一样获了科技创新奖,特别是其中的某些人,还获得了另一个奖项——特殊贡献奖。而越多的人获得了两个奖项,Miss Medusa就会越眼红。于是她决定统计有哪些人获得了两个奖项,来知道自己有多眼红。

输入格式
第一行两个整数 n,m,表示有 n 个人获得科技创新奖,m 个人获得特殊贡献奖。

第二行
n 个正整数,表示获得科技创新奖的人的编号。

第三行
m 个正整数,表示获得特殊贡献奖的人的编号。

输出格式
输出一行,为获得两个奖项的人的编号,按在科技创新奖获奖名单中的先后次序输出。

输入输出样例
输入
4 3
2 15 6 8
8 9 2
输出
2 8

 import java.util.*;
public class P15171 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        Integer[] arr1=new Integer[n];
        Integer[] arr2=new Integer[m];
        List<Integer> list=new ArrayList<>();
        for (int i = 0; i < n; i++) {
            arr1[i]=sc.nextInt();
        }
        for (int i = 0; i < m; i++) {
            arr2[i]=sc.nextInt();
        }
        Set<Integer> set=new HashSet<>();
        set.addAll(Arrays.asList(arr2));
        for (int i = 0; i < n; i++) {
            if(!set.add(arr1[i])) {System.out.print(arr1[i]+" ");}
        }
    }
}

posted @ 2024-02-28 22:20  笠大  阅读(34)  评论(0编辑  收藏  举报