题目:
思路:
- import java.util.*;
-
- // 注意类名必须为 Main, 不要有任何 package xxx 信息
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- // 注意 hasNext 和 hasNextLine 的区别
- Map
map = new HashMap<>(); - while (in.hasNextInt()) { // 注意 while 处理多个 case
- String str = in.next();
- if(!map.containsKey(str)){
- map.put(in.next(),1);
- }else{
- int i = map.get(str);
- map.put(str,i+1);
- }
- }
- for(EntrySet entry : map.get()){
- if(map.get(entry)>=)
- }
- }
- }
- import java.util.*;
-
- public class Main{
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // 读取输入
- String input = scanner.nextLine();
- // 将输入按空格分割
- String[] words = input.split(" ");
-
- // 使用Map来统计每个单词的出现频率
- Map
wordCount = new HashMap<>(); - for (String word : words) {
- wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
- }
-
- // 使用List来存储满足条件的单词
- List
keywords = new ArrayList<>(); - for (Map.Entry
entry : wordCount.entrySet()) { - if (entry.getValue() >= 3) {
- keywords.add(entry.getKey());
- }
- }
-
- // 自定义比较器,首先按频次降序排序,其次按字典序升序排序
- keywords.sort((a, b) -> {
- int freqA = wordCount.get(a);
- int freqB = wordCount.get(b);
- if (freqA != freqB) {
- return freqB - freqA;
- } else {
- return a.compareTo(b);
- }
- });
-
- // 输出结果
- for (String keyword : keywords) {
- System.out.println(keyword);
- }
-
- scanner.close();
- }
- }