1题目
2、求解
import java.util.*;
public class helloo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] str = new String[] {"abc123","abc+1234","ababab--1","abab--12345"};
Map<String,Double> map = new HashMap<>();
int len =str.length;
for(int i=0;i<len;i++){
int num=0; //统计每个字符串中数字个数
int letter=0;//统计每个字符串中字母的个数
for(int j=0;j<str[i].length();j++){
if(str[i].charAt(j)>=48 && str[i].charAt(j)<=57){
//数字
num++;
}else if((str[i].charAt(j)>=97 && str[i].charAt(j)<=122) || (str[i].charAt(j)>=65 && str[i].charAt(j)<=90)){
//大小写字母
letter++;
}
}
//
map.put(str[i],letter*1.0/num); //结果排序规则1 是字符与数字数目比值大小排序
}
// 根据 value排序
// 根据题目要求,先根据value排序,value相同的情况下,再根据 key排序
List<Map.Entry<String,Double>> list = new ArrayList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Double>>() {
@Override
public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
double ans = o2.getValue()-o1.getValue();
if(ans==0.0){
//再根据key排序
ans = o2.getKey().compareTo(o1.getKey());
}
return (int) Math.floor(ans); //注意小数,向下取整,有正负数
}
});
// 输出结果
String[] res = new String[len];
int i=0;
for(Map.Entry<String,Double> entry : list){
res[i] = entry.getKey();
i++;
}
for(int j=0;j<res.length;j++){
System.out.print(res[j] +" ");
}
}
}