题解 | #记票统计#
记票统计
http://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//没用到保证读到正确行数
in.nextLine();
String names = in.nextLine();
//没用到
in.nextLine();
String votes = in.nextLine();
String[] nameArr = names.split("\\s+");
String[] voteArr = votes.split("\\s+");
Map<String, Integer> map = new LinkedHashMap<>();
int inValid = 0;
//投票人初始化(防止0得票的数据不输出)
for(String name : nameArr){
map.put(name, 0);
}
for(String vote : voteArr){
if(containsName(nameArr, vote)){
map.put(vote, map.getOrDefault(vote, 0)+1);
}else {
inValid++;
}
}
map.forEach((k,v)->{
System.out.println(k + " : " + v);
});
System.out.println("Invalid : " + inValid);
}
private static boolean containsName(String[] nameArr, String vote) {
for(String name : nameArr){
if(name.equals(vote)){
return true;
}
}
return false;
}
}