题解 | #记票统计#

记票统计

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.hasNextInt()) { // 注意 while 处理多个 case
        //     int a = in.nextInt();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }

        Map<String, Integer> map = new LinkedHashMap<>();
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            map.put(in.next(), 0);
        }

        int p = in.nextInt();
        int count = 0;
        while (in.hasNext()) {
            String temp = in.next();
            // if ("K".equals(temp)) {
            //     int a = 0;
            // }
            Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
            for (Map.Entry<String, Integer> entry : entrySet) {
                if (entry.getKey().equals(temp)) {
                    map.put(temp, entry.getValue() + 1);
                    count++;
                }
            }
        }
        map.put("Invalid", p - count);
        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        for (Map.Entry<String, Integer> entry : entrySet) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务