题解 | #记票统计#
记票统计
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, 如果无法匹配, 则记作无效选票
OPPO公司福利 1202人发布