有谁知道全国计算机二级C语言考试最后一题编程题如果没有运行会给分吗希望有一样经历的朋友解答

2024-12-19 02:33:40
推荐回答(1个)
回答1:

import java.util.*;
public class Test {
public static void main(String[] args) {
String str = "aaaaaaaaaaaaaaaaabaadddccefqdffbaeba";
Set sets = new HashSet();
Map map = new HashMap();
for (Character c : str.toCharArray()) {
int i = 1;
if (!sets.add(c)) {
i = map.get(c);
i++;
}
map.put(c, i);
}
List> orderList = new ArrayList>(
map.entrySet());
Collections.sort(orderList,
new Comparator>() {
public int compare(Map.Entry o1,
Map.Entry o2) {
return (o1.getValue() - o2.getValue());
}
});//重写排序方法
String result="";
for (Map.Entry entry: orderList) {
result+=entry.getKey()+":"+entry.getValue()+",";
}
if(result.length()>0)
result=result.substring(0, result.length()-1);
System.out.println(result);
}
}