计票JAVA

记票统计

http://www.nowcoder.com/questionTerminal/3350d379a5d44054b219de7af6708894

JAVA

HashMap + list

map.containsKey

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while (in.hasNext()){
            int va = 0;
            int n = in.nextInt();
            LinkedList<String> list = new LinkedList<>();
            Map<String, Integer> map = new HashMap<>();
            for (int i = 0; i < n; i++){
                String cur = in.next();
                list.add(cur);
                map.put(cur, 0);
            }
            int n2 = in.nextInt();
            for (int i = 0; i < n2; i++){
                String cur = in.next();
                if (map.containsKey(cur)){
                    map.put(cur, map.get(cur) + 1);
                }else{
                    va++;
                }
            }
            for (int i = 0; i < n; i++){
                String k = list.get(i);
                int v = map.get(k);
                System.out.println(k + " : " + v);
            }
            System.out.println("Invalid" + " : " + va);
        }
    }
}
全部评论

相关推荐

10-05 11:11
海南大学 Java
投票
理想江南137:感觉挺真诚的 感觉可以试一试
点赞 评论 收藏
分享
点赞 1 评论
分享
牛客网
牛客企业服务