JD_OJ 题目1007:奥运排序问题

来源:互联网 发布:php get和post的区别 编辑:程序博客网 时间:2024/06/02 06:39

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:9186

解决:1979

题目描述:

按要求,给国家进行排名。

输入:
有多组数据。
第一行给出国家数N,要求排名的国家数M,国家号从0到N-1。
第二行开始的N行给定国家或地区的奥运金牌数,奖牌数,人口数(百万)。
接下来一行给出M个国家号。
输出:
排序有4种方式: 金牌总数 奖牌总数 金牌人口比例 奖牌人口比例 
对每个国家给出最佳排名排名方式 和 最终排名
格式为: 排名:排名方式
如果有相同的最终排名,则输出排名方式最小的那种排名,对于排名方式,金牌总数 < 奖牌总数 < 金牌人口比例 < 奖牌人口比例 
如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4.
每组数据后加一个空行。
样例输入:
4 44 8 16 6 24 8 22 12 40 1 2 34 28 10 18 11 28 12 38 13 40 3
样例输出:
1:31:12:11:21:11:1
来源:

2010年浙江大学计算机及软件工程研究生机试真题


题目分析:

真给这题跪了,在OJ上不停WA,让我抓狂,最后我舍弃了自尊才AC了。要注意的是给m个给定国家排序,而不是给所有国家排序
#include <stdio.h>#include <algorithm>using namespace std;struct Country{int id;int goldMedals;int totalMedals;int population;double rate;double rateGold;};struct Sort{int rank[1000];int id[1000];}sortArray[1000];bool cmp1(struct Country a,struct Country b){//按金牌总数排名return a.goldMedals > b.goldMedals;}bool cmp2(struct Country a,struct Country b){//按奖牌总数排名return a.totalMedals > b.totalMedals;}bool cmp3(struct Country a,struct Country b){//按金牌人口比例return a.rateGold > b.rateGold;}bool cmp4(struct Country a,struct Country b){//按奖牌人口比例return a.rate > b.rate;}void bestSort(struct Country country[],int n){int i,rank,cnt;sort(country,country+n,cmp1);rank = 1;cnt = 0;for(i=0;i<n;i++){sortArray[0].id[i] = country[i].id;if(i == 0){sortArray[0].rank[i] = rank;}if(i-1>=0 && country[i-1].goldMedals == country[i].goldMedals){sortArray[0].rank[i] = rank;cnt++;}else if(i-1 >= 0){rank++;sortArray[0].rank[i] = rank+cnt;}}sort(country,country+n,cmp2);rank = 1;cnt = 0;for(i=0;i<n;i++){sortArray[1].id[i] = country[i].id;if(i == 0){sortArray[1].rank[i] = rank;}if(i-1>=0 && country[i-1].totalMedals == country[i].totalMedals){sortArray[1].rank[i] = rank;cnt++;}else if(i-1>=0){rank++;sortArray[1].rank[i] = rank+cnt;}}sort(country,country+n,cmp3);rank = 1;cnt = 0;for(i=0;i<n;i++){sortArray[2].id[i] = country[i].id;if(i == 0){sortArray[2].rank[i] = rank;}if(i-1>=0 && country[i-1].rateGold == country[i].rateGold){sortArray[2].rank[i] = rank;cnt++;}else if(i-1>=0){rank++;sortArray[2].rank[i] = rank+cnt;}}sort(country,country+n,cmp4);rank = 1;cnt = 0;for(i=0;i<n;i++){sortArray[3].id[i] = country[i].id;if(i == 0){sortArray[3].rank[i] = rank;}if(i-1>=0 && country[i-1].rate == country[i].rate){sortArray[3].rank[i] = rank;cnt++;}else if(i-1>=0){rank++;sortArray[3].rank[i] = rank+cnt;}}}void findBestSort(int id,int n){int i,j;int bestRank = -1,method;for(i=0;i<4;i++){for(j=0;j<n;j++){if(id == sortArray[i].id[j]){if(bestRank == -1){bestRank = sortArray[i].rank[j];method = i+1;}else if(sortArray[i].rank[j] < bestRank){bestRank = sortArray[i].rank[j];method = i+1;}}}}printf("%d:%d\n",bestRank,method);}int main(){freopen("in.txt","r",stdin);int n,m,i,j;while(scanf("%d%d",&n,&m) != EOF){struct Country *country = (struct Country *)malloc(n * sizeof(Country));int *sortCountry = (int *)malloc(m * sizeof(int));for(i=0;i<n;i++){country[i].id = i;scanf("%d%d%d",&country[i].goldMedals,&country[i].totalMedals,&country[i].population);country[i].rate = (double)country[i].totalMedals / country[i].population;country[i].rateGold = (double)country[i].goldMedals / country[i].population;}struct Country *country1 = (struct Country *)malloc(m * sizeof(Country));for(i=0;i<m;i++){scanf("%d",&sortCountry[i]);for(j=0;j<n;j++){if(sortCountry[i] == country[j].id){country1[i].goldMedals = country[j].goldMedals;country1[i].id = country[j].id;country1[i].population = country[j].population ;country1[i].rate = country[j].rate ;country1[i].rateGold = country[j].rateGold ;country1[i].totalMedals = country[j].totalMedals;}}}bestSort(country1,m);for(i=0;i<m;i++){findBestSort(sortCountry[i],n);}printf("\n");}return 0;}



0 0