题解 | #记票统计#

记票统计

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 的区别
        int m = Integer.parseInt(in.next());
        LinkedHashMap<String, Integer> map = new LinkedHashMap<>();//LinkedHashMap才能保持输入顺序
        for (int i = 0; i < m; i++) {
            String s = in.next();
            map.put(s, 0);
        }
        map.put("Invalid", 0);
        int n = Integer.parseInt(in.next());
        for (int j = 0; j < n; j++) {
            String t = in.next();
            if (map.containsKey(t)) {
                map.put(t, map.get(t) + 1);
            } else {
                map.put("Invalid", map.get("Invalid") + 1);
            }
        }
        map.forEach((key, value)-> {
            System.out.println(key + " : " + value);
        });
    }
}

全部评论

相关推荐

没hc还海面!呜呜,避雷
回收旧报纸:没有海面吧,我做完笔试有一个多月了,还没消息
点赞 评论 收藏
分享
迷茫的大四🐶:那你问他上班之后老实了没
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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