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;
}
上一篇: PHP生成唯一ID