JAVA如何实现中式排名和美式排名

2019-10-17 09:09:46来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

JAVA如何实现中式排名和美式排名

根据公司需求,需要编写中式和美式排名算法,根据具体业务编写的,代码如下,看不懂留言,欢迎探讨,求高手指教更高效稳定的方法。
private static int[] datas = {9,9,10,10,9,8,4,3,3,3,3,3,4,3,44,2,2,2,2,1};

public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
for(int i = 0; i < 100; i ++){
map.put(i + "",(int)(Math.random()*100)%3+",");
}
Map<String,String> map1 = orderByChPfSort(map);
System.out.println("中式排名:");
for(Map.Entry<String,String> entry : map1.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
System.out.println("美式排名:");
Map<String,String> map2 = orderByPf(map);
for(Map.Entry<String,String> entry : map2.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}

/**
* 中式排名算法
* @param map
* @return
*/
private static Map<String,String> orderByChPfSort(Map<String, String> map){
List<Map.Entry<String, String>> lists = new ArrayList<>(map.entrySet());
//step1: 排序
Collections.sort(lists, new Comparator<Map.Entry<String, String>>() {
@Override
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
String value1 = o1.getValue();
String value2 = o2.getValue();
int length1 = value1.split(",").length;

return Double.valueOf(value1.split(",")[length1-1]).compareTo(Double.parseDouble(value2.split(",")[length1-1]));
}
});

//step2:先给第一个数第一名的位置,
//step3:再将后面的数与前一个数进行对比,如果大就获取前一个数的排名再加1,如果等于,就去前一个的排名给后面数
Map<String,String> dataSort = new HashMap<>();
int i = 0;
String firstValue = "";
String firstKey = "";
for(Map.Entry<String, String> entry : lists){
String value2 = entry.getValue();
int length2 = value2.split(",").length;
if(i == 0){
dataSort.put(entry.getKey(), value2 + 1 + ",");
}else{
if(Double.parseDouble(value2.split(",")[length2 - 1]) > Double.parseDouble(firstValue.split(",")[length2-1])){
dataSort.put(entry.getKey(),value2 + ((Integer.parseInt(dataSort.get(firstKey).split(",")[length2])) + 1) + ",");
}else if((Double.parseDouble(value2.split(",")[length2 - 1]) == Double.parseDouble(firstValue.split(",")[length2-1]))){
dataSort.put(entry.getKey(),value2 + (Integer.parseInt(dataSort.get(firstKey).split(",")[length2])) + ",");
}
}
firstValue = value2;
firstKey = entry.getKey();
i++;
}
return dataSort;
}

/**
* 评分美式排名方法
*
* @param map
* @return
*/
private static Map<String, String> orderByPf(Map<String, String> map) {
Map<String, String> zsPmMap = new TreeMap<>();
//step1:遍历map
for (Map.Entry<String, String> m : map.entrySet()) {
int size = 1;
String[] strsm = m.getValue().split(",");
int lengthm = strsm.length;
//step2:遍历map
for (Map.Entry<String, String> m1 : map.entrySet()) {
String[] strsm1 = m1.getValue().split(",");
int lengthm1 = strsm1.length;
//step3:将map中的每个值与其他值都对比一遍,如果有大于的就将排名数加1
if (Double.parseDouble(strsm[lengthm - 1]) > Double.parseDouble(strsm1[lengthm1 - 1])) {
size++;
}
}
String value = "";
//step4:重新给value设置值
for (int i = 0; i <= lengthm - 1; i++) {
value += (strsm[i] + ",");
if (i == lengthm - 1) {
value += (size + ",");
}
}
//step5:将产生的包含排序值的数据重新添加到map中
zsPmMap.put(m.getKey(), value);
}
return zsPmMap;
}

/**
* 对数组进行中式拍寻
*/
private Map<Integer,String> sortData(){
//step1: 排序
for(int i = 0; i < datas.length; i ++){
for(int j = 0; j < datas.length; j ++){
if(datas[i] < datas[j]){
int mid = datas[i];
datas[i] = datas[j];
datas[j] = mid;
}
}
}
//step2:先给第一个数第一名的位置,
//step3:再将后面的数与前一个数进行对比,如果大就获取前一个数的排名再加1,如果等于,就去前一个的排名给后面数
Map<Integer,String> dataSort = new HashMap<>();
for(int i = 0; i < datas.length ; i ++){
if(i == 0){
dataSort.put(i,datas[i] + "," + 1);
}else{
if(datas[i] > datas[i-1]){
dataSort.put(i,datas[i] + "," + (Integer.parseInt(dataSort.get(i-1).split(",")[1]) + 1));
}else if(datas[i] == datas[i-1]){
dataSort.put(i,datas[i] + "," + (Integer.parseInt(dataSort.get(i-1).split(",")[1])));
}
}
}

for (Map.Entry<Integer,String> entry : dataSort.entrySet()){
System.out.print(entry.getKey() + ":" + entry.getValue());
System.out.println();
}
return dataSort;
}


原文链接:https://www.cnblogs.com/xiaofangcunzi/p/11691525.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:spring5 源码深度解析----- 被面试官给虐懵了,竟然是因为我不懂

下一篇:SpringCloud系列-整合Hystrix的两种方式