题解 | #记票统计#

记票统计

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

import java.util.Scanner;

public class Main {
    static String[] volunteer;
    static String[] voter;
    static int[] vote;
    static int invalid;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        volunteer = new String[n];
        for (int i = 0; i < n; i++) {
            volunteer[i] = in.next();
        }
        vote = new int[n];
        int m = in.nextInt();
        voter = new String[m];
        for (int i = 0; i < m; i++) {
            voter[i] = in.next();
        }
        invalid = 0;
        for (int i = 0; i < m; i++) {
            int index = -1;
            for (int j = 0; j < n; j++) {
                if (voter[i].equals(volunteer[j])) {
                    index = j;
                }
            }
            if (index == -1) {
                invalid++;
            } else {
                vote[index]++;
            }
        }
        for (int i = 0; i < n; i++) {
            System.out.println(volunteer[i] + " : " + vote[i]);
        }
        System.out.println("Invalid : " + invalid);
    }
}

解题思路:

1, 对所有选票进行遍历, 如果能和某个候选人匹配, 则计做有效选票;

2, 如果无法匹配, 则记作无效选票

全部评论

相关推荐

LastWh1spe...:ssob真有些人和那个没睡醒一样
点赞 评论 收藏
分享
想run的马里奥在学...:这个学历帮你扫平百分之80的障碍,投就完了,这会找不到就等3月暑期一样能找到
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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