题解 | #记票统计#
记票统计
https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); //String[] names = sc.nextLine().split(" "); //不太好使 String[] names = new String[n]; for(int i=0; i<n; i++){ names[i] = sc.next(); } int m = sc.nextInt(); //String[] votes = sc.nextLine().split(" "); String[] votes = new String[m]; for(int j=0; j<m; j++){ votes[j] = sc.next(); } if(n<1 || n>100 || m<1 || m>100) return; Map<String, Integer> map = new HashMap<>(); for(String str: names){ map.put(str, 0); } int invalid = 0; Set<String> mpKeys = map.keySet(); for(String s: votes){ if(mpKeys.contains(s)){ map.replace(s, map.get(s)+1); }else{ invalid++; } } /*for(String k: mpKeys){ System.out.println(k + " : " + map.get(k)); }*/ //需要按照输入顺序输出,但是set无序 for(String key: names){ System.out.println(key + " : " + map.get(key)); } System.out.println("Invalid : " + invalid); } }