题解 | 记票统计

记票统计

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);
        // 候选人总数
        int hxrCnt = Integer.parseInt(in.nextLine());
        // 候选人名字
        String hxrStr = in.nextLine();

        // 投票人总数
        int tprCnt = Integer.parseInt(in.nextLine());
        String tprStr = in.nextLine();
        // 具体的投票
        String[] tpArray = tprStr.split(" ");

        // 无效投票的总数
        int invaCnt = 0;
        Map<String, Integer> map = new LinkedHashMap<>();
        String[] hxrs = hxrStr.split(" ");
        for (String hxr : hxrs) {
            map.put(hxr, 0);
        }
        for (String str : tpArray) {
            if (!map.keySet().contains(str)) {
                invaCnt++;
            } else {
                map.put(str, map.get(str) + 1);
            }
        }
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = iterator.next();
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
        System.out.println("Invalid : " + invaCnt);
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 15:39
点赞 评论 收藏
分享
05-12 11:09
已编辑
门头沟学院 后端
已注销:没必要放这么多专业技能的描述。这些应该是默认已会的,写这么多行感觉在凑内容。项目这块感觉再包装包装吧,换个名字,虽然大家的项目基本都是网上套壳的,但是你这也太明显了。放一个业务项目,再放一个技术项目。技术项目,例如中间件的一些扩展和尝试。
简历中的项目经历要怎么写
点赞 评论 收藏
分享
07-01 13:37
门头沟学院 Java
steelhead:不是你的问题,这是社会的问题。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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