-
题目描述:
-
按要求,给国家进行排名。
-
输入:
-
有多组数据。
第一行给出国家数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
#include <stdio.h> #include <algorithm> using namespace std; struct COUNTRY { double num_g, num_m, popu; double r_g, r_m; int rank[5]; int ID; }country[200],buf[200]; bool cmp1(COUNTRY a, COUNTRY b) { return a.num_g > b.num_g; } // 按金牌总数排序 bool cmp2(COUNTRY a, COUNTRY b) { return a.num_m > b.num_m; } // 按奖牌总数排序 bool cmp3(COUNTRY a, COUNTRY b) { return a.r_g > b.r_g; } // 按金牌人口比例排序 bool cmp4(COUNTRY a, COUNTRY b) { return a.r_m > b.r_m; } // 按奖牌人口比例排序 bool cmp5(COUNTRY a, COUNTRY b) { return a.ID < b.ID; } // 最后再按ID排序 int main() { int N, M; while(scanf("%d%d",&N,&M) != EOF) { int i,j; for(i = 0; i < N; i++) { scanf("%lf%lf%lf",&buf[i].num_g,&buf[i].num_m,&buf[i].popu); buf[i].r_g = buf[i].num_g / buf[i].popu; buf[i].r_m = buf[i].num_m / buf[i].popu; for(j = 0; j < 5; j++) buf[i].rank[j] = 0; buf[i].ID = i; } int input_M, top = -1; for(i = 0; i < M; i++) { scanf("%d",&input_M); country[++top] = buf[input_M]; } // 注意只需对要求排序的M个国家排序 sort(country,country+M,cmp1); for(i = 0; i < N; i++) { if(i == 0) country[i].rank[1] = 1; else { if(country[i].num_g == country[i-1].num_g) country[i].rank[1] = country[i-1].rank[1]; else country[i].rank[1] = i + 1; } } // 确定按第一种方式排序的名次 sort(country,country+M,cmp2); for(i = 0; i < N; i++) { if(i == 0) country[i].rank[2] = 1; else { if(country[i].num_m == country[i-1].num_m) country[i].rank[2] = country[i-1].rank[2]; else country[i].rank[2] = i + 1; } } // 确定按第二种方式排序的名次 sort(country,country+M,cmp3); for(i = 0; i < N; i++) { if(i == 0) country[i].rank[3] = 1; else { if(country[i].r_g == country[i-1].r_g) country[i].rank[3] = country[i-1].rank[3]; else country[i].rank[3] = i + 1; } } // 确定按第三种方式排序的名次 sort(country,country+M,cmp4); for(i = 0; i < N; i++) { if(i == 0) country[i].rank[4] = 1; else { if(country[i].r_m == country[i-1].r_m) country[i].rank[4] = country[i-1].rank[4]; else country[i].rank[4] = i + 1; } } // 确定按第四种方式排序的名次 sort(country,country+M,cmp5); // 再将各国按ID从小到大排序 for(i = 0; i < M; i++) { int min = N, rank_min; for(j = 1; j < 5; j++) if(country[i].rank[j] < min) { min = country[i].rank[j]; rank_min = j; } //找出最佳排序结果和排序方式 printf("%d:%d\n",min,rank_min); } printf("\n"); //题目要求每组数据后加一个空行 } return 0; }
-