题目描述
2012伦敦奥运会即将到来,大家都非常关注奖牌榜的情况,现在我们假设奖牌榜的排名规则如下:
1、首先gold medal数量多的排在前面;
2、其次silver medal数量多的排在前面;
3、然后bronze medal数量多的排在前面;
4、若以上三个条件仍无法区分名次,则以国家名称的字典序排定。
我们假设国家名称不超过20个字符、各种奖牌数不超过100,且大于等于0。
解答要求
时间限制:1000ms, 内存限制:100MB
输入
第一行输入一个整数N(0<N<21),代表国家数量;
然后接下来的N行,每行包含一个字符串Namei表示每个国家的名称,和三个整数Gi、Si、Bi表示每个获得的gold medal、silver medal、bronze medal的数量,以空格隔开,如(China 51 20 21),具体见样例输入。
输出
输出奖牌榜的依次顺序,只输出国家名称,各占一行,具体见样例输出。
样例
输入样例 1 复制
5
China 32 28 34
England 12 34 22
France 23 33 2
Japan 12 34 25
Rusia 23 43 0
输出样例 1
China
Rusia
France
Japan
England
思路:模拟题,按照题目给出的排序规则进行排序即可
class CountryInfo{
String countryName;
int gold;
int silver;
int bronze;
public CountryInfo(String countryName,int gold,int silver,int bronze){
this.countryName = countryName;
this.gold = gold;
this.silver = silver;
this.bronze = bronze;
}
}
public class Main {
public static void main(String[] args){
int N = 5;
CountryInfo[] countryInfos = new CountryInfo[N];
countryInfos[0] = new CountryInfo("China", 32, 28, 34);
countryInfos[1] = new CountryInfo("England", 12, 34, 22);
countryInfos[2] = new CountryInfo("France", 23, 33, 2);
countryInfos[3] = new CountryInfo("Japan", 12, 34, 25);
countryInfos[4] = new CountryInfo("Rusia", 23, 43, 0);
find(countryInfos);
}
public static void find(CountryInfo[] countryInfos){
Arrays.sort(countryInfos,(a,b)->{
//易错点 != 写成了 >
if(b.gold != a.gold){
return b.gold - a.gold;
}
if(b.silver != a.silver){
return b.silver - a.silver;
}
if(b.bronze != a.bronze){
return b.bronze - a.bronze;
}
String sa = a.countryName;
String sb = b.countryName;
int n1 = sa.length();
int n2 = sb.length();
int index = 0;
while(index < n1 && index < n2){
//易错点当不一样时应该直接返回值
if (sa.charAt(index) != sb.charAt(index)) {
return sa.charAt(index) - sb.charAt(index);
}
index++;
}
return n1 - n2;
});
for (int i = 0; i < countryInfos.length; i++) {
System.out.println(countryInfos[i].countryName);
}
}
}