Tag: 频率分布

排序字符的频率

我刚刚制作了一个算法来计算字符串中字符的频率。 令我困惑的是如何对频率进行排序,以便在顶部列出具有最大出现次数的字符,在底部列出最少的字符。 起初我尝试使用另一个变量’fc’(对于频率计数器)与我原来的计数器变量’k’一致。 然而,我陷入了如何对这个频率进行排序的思考过程中,我所做的fc var是没用的。 感谢您提供的任何帮助! 这是我的代码: import java.io.*; public class Freq { public static void main(String args[])throws IOException { //read input stream BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int ci,i,j,k,l,fc;l=0; String str,str1; char c,ch; System.out.println(“Enter your String”); str=in.readLine(); i=str.length(); //cycle through ASCII table chars and obtain chars typed for(c=’A’;c<='z';c++) { k=0; fc=0; //fc keeps count like […]