九度oj-1007 奥运排序问题
时间限制:1 秒 内存限制:32 兆
- 题目描述:
-
按要求,给国家进行排名。
- 输入:
-
有多组数据。
第一行给出国家数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
- 来源:
- 2010年浙江大学计算机及软件工程研究生机试真题
- 思路:对于选定的国家进行排序时,不用传统排序方法,而是只统计每个国家的排名,不移动国家元素。
-
1 #include<iostream> 2 #include<cstdlib> 3 #include<algorithm> 4 #include<cstdio> 5 using namespace std; 6 const int maxn=1000; 7 8 struct country{ 9 int id; //国家编号 10 int gold; //金牌总数 11 int award; //奖牌总数 12 int population; //人口总数 13 double goldratio; //金牌均占比 14 double awardratio; //奖牌均占比 15 int irank; //临时排名 16 int frank; //最终最好排名 17 int type; //最好排名方式 18 }countries[maxn],Rank[maxn]; 19 20 int main() 21 { 22 int N,M; 23 while(scanf("%d%d",&N,&M)!=EOF) 24 { 25 for(int i=0;i<N;i++) 26 { 27 scanf("%d%d%d",&countries[i].gold,&countries[i].award,&countries[i].population); 28 countries[i].goldratio=(double)countries[i].gold/countries[i].population; 29 countries[i].awardratio=(double)countries[i].award/countries[i].population; 30 } 31 32 int number; 33 for(int i=0;i<M;i++) 34 { 35 scanf("%d",&number); 36 Rank[i]=countries[number]; 37 //printf("%d %d %d\n",Rank[i].gold,Rank[i].award,Rank[i].population); 38 Rank[i].frank=0; 39 Rank[i].irank=0; 40 } 41 42 int nCount; 43 44 //按金牌总数排名 45 for(int i=0;i<M;i++) 46 { 47 nCount=1; 48 for(int j=0;j<M;j++) 49 { 50 if(Rank[i].gold<Rank[j].gold) 51 nCount++; 52 } 53 Rank[i].frank=nCount; 54 Rank[i].type=1; 55 } 56 //printf("%d %d\n",Rank[2].frank,Rank[2].type); 57 58 //按奖牌总数排名 59 for(int i=0;i<M;i++) 60 { 61 nCount=1; 62 for(int j=0;j<M;j++) 63 { 64 if(Rank[i].award<Rank[j].award) 65 nCount++; 66 } 67 if(nCount<Rank[i].frank) 68 {Rank[i].frank=nCount;Rank[i].type=2;} 69 } 70 71 //按金牌均占比排名 72 for(int i=0;i<M;i++) 73 { 74 nCount=1; 75 for(int j=0;j<M;j++) 76 { 77 if(Rank[i].goldratio<Rank[j].goldratio) 78 nCount++; 79 } 80 if(nCount<Rank[i].frank) 81 {Rank[i].frank=nCount;Rank[i].type=3;} 82 } 83 84 //按奖牌均占比排名 85 for(int i=0;i<M;i++) 86 { 87 nCount=1; 88 for(int j=0;j<M;j++) 89 { 90 if(Rank[i].awardratio<Rank[j].awardratio) 91 nCount++; 92 } 93 if(nCount<Rank[i].frank) 94 {Rank[i].frank=nCount;Rank[i].type=4;} 95 } 96 97 //最后输出结果 98 for(int i=0;i<M;i++) 99 { 100 printf("%d:%d\n",Rank[i].frank,Rank[i].type); 101 } 102 printf("\n"); 103 } 104 return 0; 105 }