按出现次数排序

问题描述 投票:0回答:2

例如,给我一个单词,我必须按照该单词出现的次数对它的字母进行排序,如果2个字母出现的次数相同,则将按字典序最小排序。现在,我已经开始看到一个单词中出现了多少次字母,但是从这里开始我不知道该怎么做。问题要求我使用BufferedReader和BufferedWriter。

import java.util.*;

public class Main {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Map<Character, Integer> m = new HashMap<>();
    String s = sc.nextLine();
    for (int i = 0; i < s.length(); ++i) {
        char c = s.charAt(i);
        if (m.containsKey(c))
            m.put(c, m.get(c) + 1);
        else
            m.put(c, 1);
    }
    for (char letter = 'a'; letter <= 'z'; ++letter)
        if (m.containsKey(letter))
            System.out.println(letter + ": " + m.get(letter));
}

目前,我要发布单词中出现频率最高的字母,但是我不知道如何按照出现的次数对它们进行排序,并且如果有两个字母出现的次数相同且词典词典最小, 。

java bufferedreader bufferedwriter
2个回答
0
投票

要计算单词中的字母,您可以使用更简单的方法:

用26个零定义数组,扫描输入行并在此数组中增加适当的索引,因此如果遇到'a'(或'A'-相同的字母,但符号不同)-您将在索引0处增加值,b-索引1,等等

在此扫描期间,您还可以计算出大多数出现的符号,如下所示:

public static void main(final String[] args) throws IOException {
    char maxSymbol = 0;
    int maxCount = 0;

    final int[] counts = new int[26]; // number of times each letter (a-z) appears in string
    try (final BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
        final String s = br.readLine().toLowerCase(); // calculate case-insensitive counts
        for (final char c : s.toCharArray()) {
            final int idx = c - 'a'; // convert form ASCII code to 0-based index
            counts[idx]++;

            if (counts[idx] > maxCount) {
                maxSymbol = c; // we found most occurred symbol for the current moment
                maxCount = counts[idx];
            } else if (counts[idx] == maxCount) { // we found 2nd most occurred symbol for the current moment, need to check which one is minimal in lexicographical order
                if (c < maxSymbol) {
                    maxSymbol = c;
                }
            }
        }
    }

    if (maxSymbol > 0) {
        System.out.println("Most frequent symbol " + maxSymbol + " occurred " + maxCount);
    }
}

我已经使用缓冲的读取器从stdin获取数据,但是我不知道将缓冲的写入器放在哪里,也许要打印结果?


0
投票

不清楚BufferedReader

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    Map<Character, Integer> m = new HashMap<>();
    String testString = sc.nextLine();
    Map<Character, Integer> map = new HashMap<>();

    for (int i = 0; i < testString.length(); i++) {
        char someChar = testString.charAt(i);
        if (someChar == ' ') {
            continue;
        }
        int count = map.getOrDefault(someChar, 0);
        map.put(testString.charAt(i), count + 1);
    }
    List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());

    list.sort((o1, o2) -> {
        if (o1.getValue().equals(o2.getValue())) {
            return 1;/// your lexicographic comparing
        }
        return o2.getValue() - o1.getValue();
    });

    list.forEach(entry -> System.out.println(entry.getKey() + "  " + entry.getValue()));
}
© www.soinside.com 2019 - 2024. All rights reserved.