关于金银铜牌排序问题

dracularking 2013-01-26 01:17:49
金牌榜排序,众所周知,按金银铜牌数量为权重次序依次排序。

但如果先按金牌数量排序,碰到金牌数量一样的国家,还需要对金牌数是否相同进行额外判断,再排银牌,铜牌。

反之则无需额外判断,按铜牌-银牌-金牌的顺序依次排序。

请问为什么反之无需额外判断,要说出根本原因。


...全文
1255 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dracularking 2013-01-28
  • 打赏
  • 举报
回复
引用 4 楼 SmallYamateh 的回复:
“权重低的先排”,这是基数排序的LSD法则;比如你要排序50个大小不同的两位数,先要建立10个桶,用链表的尾插法进行个位数的排序;然后建立10个新桶,遍历这10个桶对应的链表,继续用尾插法对十位数进行排序。这就是你说的“将低位关键字要排序的先给排序好了,导致进行高位关键字排序时已经不用排序了”,和LSD法则是完全对应的。
感谢,获益良多!
kosora曹 2013-01-27
  • 打赏
  • 举报
回复
“权重低的先排”,这是基数排序的LSD法则;比如你要排序50个大小不同的两位数,先要建立10个桶,用链表的尾插法进行个位数的排序;然后建立10个新桶,遍历这10个桶对应的链表,继续用尾插法对十位数进行排序。这就是你说的“将低位关键字要排序的先给排序好了,导致进行高位关键字排序时已经不用排序了”,和LSD法则是完全对应的。
dracularking 2013-01-27
  • 打赏
  • 举报
回复
引用 2 楼 SmallYamateh 的回复:
难道是我理解错了?你看看我的代码是不是你想要的:
你好,不是要代码,排序怎么排都行,是先按铜牌排序可以不用判断哪些奖牌相等的原因,这个我后来想了想,是权重低的先排,等于是将最后可能要排序的先给排序好了,导致需要排序时已经不用排序了,是不是这个原因...
kosora曹 2013-01-27
  • 打赏
  • 举报
回复


难道是我理解错了?你看看我的代码是不是你想要的:


import java.util.Comparator;
import java.util.TreeSet;

import org.junit.Test;

/**
* 国家
*
*/
class Country{
private String name;
private int jin;
private int yin;
private int tong;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getJin() {
return jin;
}
public void setJin(int jin) {
this.jin = jin;
}
public int getYin() {
return yin;
}
public void setYin(int yin) {
this.yin = yin;
}
public int getTong() {
return tong;
}
public void setTong(int tong) {
this.tong = tong;
}
public Country(String name, int jin, int yin, int tong) {
super();
this.name = name;
this.jin = jin;
this.yin = yin;
this.tong = tong;
}
public Country() {
super();
}
@Override
public String toString() {
return "Country [国家=" + name + ", 金牌=" + jin + ", 银牌=" + yin
+ ", 铜牌=" + tong + "]";
}


}

/**
*自定义比较器
*
*/
class MyCompare implements Comparator<Country>{
public int compare(Country o1, Country o2) {
int jinCha=o1.getJin()-o2.getJin();
if(jinCha!=0){
return -jinCha;
}else{
int yinCha=o1.getYin()-o2.getYin();
if(yinCha!=0){
return -yinCha;
}else{
int tongCha=o1.getTong()-o2.getTong();
if(tongCha!=0){
return -tongCha;
}else{
return o1.getName().compareTo(o2.getName());
}
}
}
}

}
public class GoldSort {
public void sortAndPrint(){
TreeSet<Country> set=new TreeSet<Country>(new MyCompare());
Country usa=new Country("美国", 46, 29, 29);
Country cha=new Country("中国", 38, 27, 23);
Country us=new Country("英国", 29, 17, 19);
Country rus=new Country("俄罗斯", 24, 26, 32);
Country kor=new Country("韩国", 13, 8, 7);
Country gem=new Country("德国", 11, 19, 14);
Country fra=new Country("法国", 11, 11, 12);
Country ita=new Country("意大利", 8, 9, 11);
Country mag=new Country("匈牙利", 8, 4, 5);
Country aus=new Country("澳大利亚", 7, 16, 12);
//乱序加入
set.add(gem);
set.add(aus);
set.add(mag);
set.add(fra);
set.add(rus);
set.add(us);
set.add(ita);
set.add(cha);
set.add(usa);
set.add(kor);
System.out.println("输出金牌榜:");
for(Country c:set){
System.out.println(c);
}
}
@Test
public void test(){
this.sortAndPrint();
}

}



结果:
输出金牌榜:
Country [国家=美国, 金牌=46, 银牌=29, 铜牌=29]
Country [国家=中国, 金牌=38, 银牌=27, 铜牌=23]
Country [国家=英国, 金牌=29, 银牌=17, 铜牌=19]
Country [国家=俄罗斯, 金牌=24, 银牌=26, 铜牌=32]
Country [国家=韩国, 金牌=13, 银牌=8, 铜牌=7]
Country [国家=德国, 金牌=11, 银牌=19, 铜牌=14]
Country [国家=法国, 金牌=11, 银牌=11, 铜牌=12]
Country [国家=意大利, 金牌=8, 银牌=9, 铜牌=11]
Country [国家=匈牙利, 金牌=8, 银牌=4, 铜牌=5]
Country [国家=澳大利亚, 金牌=7, 银牌=16, 铜牌=12]

kosora曹 2013-01-26
  • 打赏
  • 举报
回复
基数排序?这个问题类似于“900和299谁大”。金牌是最高位、银牌次位、铜牌是最低位,奖牌榜用的应该是最高位优先法。如果是Java的话,重写Comparator的Compare方法,然后把一系列的国家放入TreeSet,输出TreeSet就可以看到奖牌榜了。

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧