给定一个list ,求出list中数据在整list的平均排名
比如 :
源数据
0.35
0.26
2.4
0.26
0.26
1.5
0.35
1.5
2.4
8.9
求出结果:
1 8.9 1
2 2.4 1.5
3 2.4 1.5
4 1.5 4.5
5 1.5 4.5
6 0.35 6
7 0.26 8.5
8 0.26 8.5
9 0.26 8.5
10 0.26 8.5
简单说明:比如0.26,在整个list中,它的排名(不分先后)有7,8,9,10,所以(7+8+9+10)/4=8.5,其他类同!
求一简单的算法,谢谢!
比如 :
源数据
0.35
0.26
2.4
0.26
0.26
1.5
0.35
1.5
2.4
8.9
求出结果:
1 8.9 1
2 2.4 1.5
3 2.4 1.5
4 1.5 4.5
5 1.5 4.5
6 0.35 6
7 0.26 8.5
8 0.26 8.5
9 0.26 8.5
10 0.26 8.5
简单说明:比如0.26,在整个list中,它的排名(不分先后)有7,8,9,10,所以(7+8+9+10)/4=8.5,其他类同!
求一简单的算法,谢谢!
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;public class PaiMing { double[] list = null;
public static void main(String[] args) {
double[] list = {0.26, 0.26, 2.4, 0.26, 0.26, 1.5, 0.35, 1.5, 2.4, 8.9};
PaiMing pm = new PaiMing(list);
pm.getResult();
}
public PaiMing(double[] list){
this.list = list;
}
private void sort(double[] org) {
List<Double> list = new ArrayList<Double>();
for (int i = 0; i < org.length; i++) {
list.add(org[i]);
}
Collections.sort(list, new Comparator<Double>(){
public int compare(Double arg0, Double arg1) {
return arg0>=arg1?-1:1;
}
});
for (int i = 0; i < org.length; i++) {
org[i] = list.get(i);
}
}
public void getResult(){
sort(list);
HashMap<Double, Double> hmSum = new HashMap<Double, Double>();
HashMap<Double, Integer> hmCount = new HashMap<Double, Integer>();
for(int i=0; i<list.length; i++){
hmSum.put(list[i], (hmSum.get(list[i])==null?0:hmSum.get(list[i])) + i + 1);
hmCount.put(list[i], (hmCount.get(list[i])==null?0:hmCount.get(list[i])) + 1);
}
for(int i=0; i<list.length; i++){
double key = list[i];
System.out.println((i+1) + " " + key + " " + hmSum.get(key)/hmCount.get(key));
}
}}