简单的不能再简单

记票统计

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

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

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int peopleNum = scanner.nextInt();
        LinkedHashMap<String, Integer> map = new LinkedHashMap<>();

        for (int i = 0; i < peopleNum; i++) {
            // 添加名字和所得票数
            map.put(scanner.next(), 0);
        }

        map.put("Invalid", 0);

        int ticketNum = scanner.nextInt();
        for (int i = 0; i < ticketNum; i++) {
            String name = scanner.next();
            if (map.get(name) == null) {
                map.put("Invalid", map.get("Invalid") + 1);
                continue;
            }
            map.put(name, map.get(name) + 1);
        }

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }

    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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