题解 | #记票统计#
记票统计
https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
int numCount = 0;
int total = 0;
int hCount = 0;
Scanner in = new Scanner(System.in);
List<String> keys = new LinkedList<>();
// 注意 hasNext 和 hasNextLine 的区别
Map<String, Integer> map = new HashMap<>();
while (in.hasNext()) { // 注意 while 处理多个 case
boolean has = in.hasNextInt();
String a = in.next();
if(has){
numCount++;
if(numCount == 1){
hCount = Integer.valueOf(a);
} else if (numCount == 2){
total = Integer.valueOf(a);
}
continue;
}
if(numCount == 1){
keys.add(a);
map.put(a, 0);
} else if (numCount == 2){
boolean c = map.containsKey(a);
if(c){
map.put(a, map.getOrDefault(a, 0)+1);
} else {
map.put("Invalid", map.getOrDefault("Invalid", 0)+1);
}
}
}
boolean c1 = map.containsKey("Invalid");
if(!c1){
map.put("Invalid", 0);
}
keys.add("Invalid");
keys.forEach(i -> {
System.out.println(i +" : "+ map.get(i));
});
}
}

