题解 | HJ94#记票统计#
记票统计
https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<String> list = new ArrayList<>(); Map<String, Integer> map = new HashMap<>(); //候选人数量 int m = scanner.nextInt(); for (int i = 0; i < m; i++) { String next = scanner.next(); //初识票数为0 map.put(next, 0); list.add(next); } // 投票数 int n = scanner.nextInt(); for (int i = 0; i < n; i++) { String nextTicket = scanner.next(); map.put(nextTicket, map.getOrDefault(nextTicket, 0) + 1); } //按格式输出 Set<Map.Entry<String, Integer>> entrySet = map.entrySet(); // 有用票数 int usefulTick = 0; for (String s : list) { for (Map.Entry<String, Integer> entry : entrySet) { if (entry.getKey().equals(s)) { System.out.print(entry.getKey() + " " + ":" + " " + entry.getValue()); usefulTick += entry.getValue(); } } System.out.println(); } //无效票数= 总票数-有用票数 int uselessTick = n - usefulTick; System.out.print("Invalid" + " " + ":" + " " + uselessTick); } }