题解 | #记票统计#
记票统计
https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
import java.util.Scanner; import java.util.LinkedHashMap; import java.util.HashMap; import java.util.Map; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Map<String, Integer> ren = new LinkedHashMap<>(); for (int i = 0; i < n; i++) { ren.put(sc.next(), 0); } int p = sc.nextInt(); int Invalid = 0; //无效票 for (int i = 0; i < p; i++) { String tou = sc.next(); //上去投票 if (ren.containsKey(tou)) { ren.put(tou, ren.get(tou) + 1); } else { Invalid++; } } for (String k : ren.keySet()) { System.out.println(k + " : " + ren.get(k)); } System.out.println("Invalid" + " : " + Invalid); } }