题解 | #记票统计#

记票统计

https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            in.nextLine(); //换行符
            String[] peo = in.nextLine().split(" ");
            //map需要使用LinkedHashMap来保证顺序
            Map<String, Integer> map = new LinkedHashMap<>();
            for(String s : peo){
                map.put(s, 0);
            }

            int m = in.nextInt(); 
            in.nextLine(); //换行符
            String[] poll = in.nextLine().split(" ");
            
            int invalid = 0;
            for(String s : poll){
                if(map.keySet().contains(s)){
                    map.put(s, map.get(s) + 1);
                }else{
                    invalid++;
                }
            }

            for(String k : map.keySet()){
                System.out.println(k + " : " + map.get(k));
            }
            System.out.println("Invalid : " + invalid);
        }
    }
}

全部评论

相关推荐

头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务