题目1007:奥运排序问题
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:6256
解决:1316
-
题目描述:
-
按要求,给国家进行排名。
-
输入:
-
有多组数据。
第一行给出国家数N,要求排名的国家数M,国家号从0到N-1。
第二行开始的N行给定国家或地区的奥运金牌数,奖牌数,人口数(百万)。
接下来一行给出M个国家号。
-
输出:
-
排序有4种方式: 金牌总数 奖牌总数 金牌人口比例 奖牌人口比例
对每个国家给出最佳排名排名方式 和 最终排名
格式为: 排名:排名方式
如果有相同的最终排名,则输出排名方式最小的那种排名,对于排名方式,金牌总数 < 奖牌总数 < 金牌人口比例 < 奖牌人口比例
如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4.
每组数据后加一个空行。
-
样例输入:
-
4 4 4 8 1 6 6 2 4 8 2 2 12 4 0 1 2 3 4 2 8 10 1 8 11 2 8 12 3 8 13 4 0 3
-
样例输出:
-
1:3 1:1 2:1 1:2 1:1 1:1
-
参考代码:
-
import java.util.Arrays; import java.util.Scanner; //国家类 class Nation{ int Gold_Medal; int Medal; int Rate; int sortWay; double NumOfPeople; double GmPerPerson; double MedalPerPerson; public Nation(int goldmetal,int metal,double numofpeople){ this.setGoldMetal(goldmetal); this.setMetal(metal); this.setNumOfPeople(numofpeople); this.setGmPerPerson(goldmetal,numofpeople); this.setMetalPerPerson(metal,numofpeople); } public void setMetalPerPerson(int metal, double numofpeople2) { this.MedalPerPerson=metal/numofpeople2; } public void setGmPerPerson(int goldmetal, double numofpeople2) { this.GmPerPerson=goldmetal/numofpeople2; } public void setNumOfPeople(double numofpeople2) { // TODO Auto-generated method stub this.NumOfPeople=numofpeople2; } public void setMetal(int metal) { // TODO Auto-generated method stub this.Medal=metal; } public void setGoldMetal(int goldmetal) { // TODO Auto-generated method stub this.Gold_Medal=goldmetal; } public double getMetalPerPerson() { return this.MedalPerPerson; } public double getGmPerPerson() { return this.GmPerPerson; } public double getNumOfPeople() { return this.NumOfPeople; } public int getMetal() { return this.Medal; } public int getGoldMetal() { return this.Gold_Medal; } public void setRate(int i, int j) { this.Rate = i; this.sortWay = j; } public int getRate() { return this.Rate; } public int getsortWay() { return this.sortWay; } } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int num_of_countries = sc.nextInt(); //国家数 int num_of_countriesneed = sc.nextInt(); //需要排序的国家数 Nation[] nations = new Nation[num_of_countries]; Nation[] nations2 = new Nation[num_of_countriesneed]; for(int i=0;i<num_of_countries;i++){ int goldmetal = sc.nextInt(); int metal = sc.nextInt(); int numofpeople = sc.nextInt(); nations[i] = new Nation(goldmetal, metal, numofpeople); //创建国家对象数组 } for(int i=0;i<num_of_countriesneed;i++){ int count = sc.nextInt(); nations2[i] = nations[count]; //需要排序的国家 } //拷贝数组后排序原数组 Nation[] nations_pre = Arrays.copyOf(nations2, nations2.length); Sort_Of_Nation(nations2); for(int i=0;i<num_of_countriesneed;i++) { System.out.println((nations_pre[i].getRate()+1)+":"+nations_pre[i].getsortWay()); } System.out.println(); } } public static void Sort_Of_Nation(Nation[] nations){ int len=nations.length; //冒泡排序 for(int i = 0;i<len;i++){ for(int j=i;j<len;j++) if(nations[i].getGoldMetal() < nations[j].getGoldMetal()){ Nation temp = nations[i]; nations[i] = nations[j]; nations[j] = temp; } } //以下四段违反DRY原则,需要改进 for(int i = 0;i<len;i++){ int rate; if(i!=0 && nations[i].getGoldMetal()==nations[i-1].getGoldMetal()) rate=nations[i-1].getRate(); else rate = i; nations[i].setRate(rate,1); } for(int i = 0;i<len;i++){ for(int j=i;j<len;j++) if(nations[i].getMetal() < nations[j].getMetal()){ Nation temp = nations[i]; nations[i] = nations[j]; nations[j] = temp; } } for(int i = 0;i<len;i++){ int rate; if(i!=0 && nations[i].getMetal()==nations[i-1].getMetal()) rate=nations[i-1].getRate(); else rate = i; if(nations[i].getRate()>rate) nations[i].setRate(rate,2); } for(int i = 0;i<len;i++){ for(int j=i;j<len;j++) if(nations[i].getGmPerPerson() < nations[j].getGmPerPerson()){ Nation temp = nations[i]; nations[i] = nations[j]; nations[j] = temp; } } for(int i = 0;i<len;i++){ int rate; if(i!=0 && nations[i].getGmPerPerson()==nations[i-1].getGmPerPerson()) rate=nations[i-1].getRate(); else rate = i; if(nations[i].getRate()>rate) nations[i].setRate(rate,3); } for(int i = 0;i<len;i++){ for(int j=i;j<len;j++) if(nations[i].getMetalPerPerson() < nations[j].getMetalPerPerson()){ Nation temp = nations[i]; nations[i] = nations[j]; nations[j] = temp; } } for(int i = 0;i<len;i++){ int rate; if(i!=0 && nations[i].getMetalPerPerson()==nations[i-1].getMetalPerPerson()) rate=nations[i-1].getRate(); else rate = i; if(nations[i].getRate()>rate) nations[i].setRate(rate,4); } } }
想法是建立国家类,然后建立国家对象,按照四种方式进行排序后,对每种排序的名次进行比较,输出结果。