lotus

贵有恒何必三更眠五更起 最无益只怕一日曝十日寒

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1. 题目

读题

HJ94 记票统计

 

 

考查点

 

2. 解法

思路

 

代码逻辑

 

具体实现

public class HJ094 {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = Integer.valueOf(sc.nextLine());
String[] candidates = sc.nextLine().split("\\s+");

int n = Integer.valueOf(sc.nextLine());
String[] votes = sc.nextLine().split("\\s+");

count(candidates, votes);
}

public static void count(String[] candidates, String[] votes) {
String temp = "Invalid";
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < candidates.length; i++) {
map.put(candidates[i], 0);
}
for (int i = 0; i < votes.length; i++) {
if (map.containsKey(votes[i])) {
map.put(votes[i], map.get(votes[i]) + 1);
} else {
map.put(temp, map.getOrDefault(temp, 0) + 1);
}
}

for (String key : candidates) {
System.out.println(key + " : " + map.get(key));
}
System.out.println(temp + " : " + map.getOrDefault(temp,0));

}
}

3. 总结

posted on 2023-07-12 01:49  白露~  阅读(43)  评论(0编辑  收藏  举报