题解 | #记票统计#

记票统计

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

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n1 = sc.nextInt();
        Map<String, Integer> map = new LinkedHashMap<>();
        for (int i = 0; i < n1; i++) {
            map.put(sc.next(), 0);
        }
        map.put("Invalid", 0);
        
        int n2 = sc.nextInt();
        for (int i = 0; i < n2; i++) {
            String key = sc.next();
            if (map.containsKey(key)) {
                map.merge(key, 1, (oldVal, newVal) -> oldVal + 1);
            } else {
                map.merge("Invalid", 1, (oldVal, newVal) -> oldVal + 1);
            }
        }
        for (Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}

全部评论

相关推荐

写不来代码的小黑:这么小的城市能有做it的公司也不容易
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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