题解 | #记票统计#
记票统计
https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
import java.util.Scanner; import java.util.Map; import java.util.HashMap; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String l1 = in.nextLine(); String l2 = in.nextLine(); String l3 = in.nextLine(); String l4 = in.nextLine(); printResult(l1,l2,l3,l4); } } private static void printResult(String s1,String s2,String s3,String s4){ Map<String,Integer> map = new HashMap<String,Integer>(); int n1 = Integer.parseInt(s1); int n2 = Integer.parseInt(s3); String [] str = s2.split(" "); String [] str1 = s4.split(" "); for(int i=0;i<n1;i++){ map.put(str[i],0); } map.put("Invalid",0); for(int j=0;j<n2;j++){ String k = str1[j]; if(map.containsKey(k)){ map.put(k,map.getOrDefault(k,0)+1); }else{ k = "Invalid"; map.put(k,map.getOrDefault(k,0)+1); } } for(String str2 : str){ System.out.println(str2+" : "+map.get(str2)); } System.out.println("Invalid"+" : "+map.get("Invalid")); } }
}