码迷,mamicode.com
首页 > 编程语言 > 详细

九度oj 题目1007:奥运排序问题

时间:2016-07-09 22:06:59      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:

九度oj 题目1007:奥运排序问题   恢复

题目描述:                       

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

输入:                       
有多组数据。 第一行给出国家数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 这道题不难,但我也提交了N次才通过,悲哀 原先的代码是这样
  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cstring>
  4 #include <string>
  5 #include <algorithm>
  6 #define MAX 1000
  7 #define BASIC 1
  8 struct country{
  9     int goldNum;
 10     int medalNum;
 11     int population;
 12     double goldRatio;
 13     double medalRatio;
 14     int placeGold;
 15     int placeMedal;
 16     int placeGR;
 17     int placeMR;
 18     int num;
 19 };
 20 country old[MAX];
 21 country cal[MAX];
 22 
 23 int compareGOLD(const void* a, const void *b) {
 24     return (*(country*)b).goldNum - (*(country*)a).goldNum;
 25 }
 26 
 27 int compareMEDAL(const void* a, const void *b) {
 28     return (*(country*)b).medalNum - (*(country*)a).medalNum;
 29 }
 30 int comparegoldRatio(const void* a, const void *b) {
 31     return (*(country*)b).goldRatio - (*(country*)a).goldRatio;
 32 }
 33 int comparemedalRatio(const void* a, const void *b) {
 34     return (*(country*)b).medalRatio - (*(country*)a).medalRatio;
 35 }
 36 
 37 int compareNum(const void* a, const void *b) {
 38     return (*(country*)a).num - (*(country*)b).num;
 39 }
 40 
 41 int main(int argc, char const *argv[])
 42 {
 43     int N,M;
 44     //freopen("input.txt","r",stdin);
 45     while(scanf("%d %d",&N,&M) != EOF) {
 46         for(int i = 0; i < N; i++) {
 47             scanf("%d %d %d",&old[i].goldNum,&old[i].medalNum,&old[i].population);
 48         }
 49         for(int i = 0; i < M; i++) {
 50             int toCal;
 51             scanf("%d",&toCal);
 52             cal[i] = old[toCal];
 53             cal[i].goldRatio = (double)cal[i].goldNum/(double)cal[i].population;
 54             cal[i].medalRatio = (double)cal[i].medalRatio/(double)cal[i].population;
 55             cal[i].placeGold = cal[i].placeMedal = cal[i].placeGR = cal[i].placeMR = -1;
 56             cal[i].num = i;
 57         }
 58         qsort(cal,M,sizeof(country),compareGOLD);
 59         int place = 1;
 60         cal[0].placeGold = 1;
 61         for(int i = 1; i < M; i++) {
 62             if(cal[i].goldNum != cal[i-1].goldNum) {
 63                 place++;
 64             }
 65             cal[i].placeGold = place;
 66         }
 67         
 68             
 69         
 70         qsort(cal,M,sizeof(country),compareMEDAL);
 71         place = 1;
 72         cal[0].placeMedal = 1;
 73         for(int i = 1; i < M; i++) {
 74             if(cal[i].medalNum != cal[i-1].medalNum) {
 75                 place++;
 76             }
 77             cal[i].placeMedal = place;
 78         }
 79 
 80         qsort(cal,M,sizeof(country),comparegoldRatio);
 81         place = 1;
 82         cal[0].placeGR = 1;
 83         for(int i = 1; i < M; i++) {
 84             if(cal[i].goldRatio != cal[i-1].goldRatio) {
 85                 place++;
 86             }
 87             cal[i].placeGR = place;
 88         }
 89 
 90         qsort(cal,M,sizeof(country),comparemedalRatio);
 91         place = 1;
 92         cal[0].placeMR = 1;
 93         for(int i = 1; i < M; i++) {
 94             if(cal[i].placeMR != cal[i-1].placeMR) {
 95                 place++;
 96             }
 97             cal[i].placeMR = place;
 98         }
 99 
100         qsort(cal,M,sizeof(country),compareNum);
101         for(int i = 0; i < M; i++) {
102             int min = cal[i].placeGold;
103             int way = BASIC;
104             if(cal[i].placeMedal < min) {
105                 min = cal[i].placeMedal;
106                 way = BASIC + 1;
107             }
108             if(cal[i].placeGR< min) {
109                 min = cal[i].placeGR;
110                 way = BASIC + 2;
111             }
112             if(cal[i].placeMR< min) {
113                 min = cal[i].placeMR;
114                 way = BASIC + 3;
115             }
116             printf("%d:%d\n",min,way);
117         }
118         printf("\n");
119         
120         /*for(int i = 0; i < M; i++) {
121             
122             printf("%d %d %d\n",cal[i].num,cal[i].goldNum,cal[i].placeGold);
123         }
124         printf("\n");*/
125     }    
126     return 0;
127 }

这段代码犯了两个错误,一是54行粗心的把medalRatio算错,二是计算排名时算错,按照题目要求,如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4. 而我的程序写出来则是1,2,2,3 改正后的代码如下:

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cstring>
  4 #include <string>
  5 #include <algorithm>
  6 #define MAX 1005
  7 #define BASIC 1
  8 struct country{
  9     int goldNum;
 10     int medalNum;
 11     int population;
 12     double goldRatio;
 13     double medalRatio;
 14     int placeGold;
 15     int placeMedal;
 16     int placeGR;
 17     int placeMR;
 18     int num;
 19 };
 20 country old[MAX];
 21 country cal[MAX];
 22 
 23 int compareGOLD(const void* a, const void *b) {
 24     return (*(country*)b).goldNum - (*(country*)a).goldNum;
 25 }
 26 
 27 int compareMEDAL(const void* a, const void *b) {
 28     return (*(country*)b).medalNum - (*(country*)a).medalNum;
 29 }
 30 int comparegoldRatio(const void* a, const void *b) {
 31     return (*(country*)b).goldRatio - (*(country*)a).goldRatio;
 32 }
 33 int comparemedalRatio(const void* a, const void *b) {
 34     return (*(country*)b).medalRatio - (*(country*)a).medalRatio;
 35 }
 36 
 37 int compareNum(const void* a, const void *b) {
 38     return (*(country*)a).num - (*(country*)b).num;
 39 }
 40 
 41 int main(int argc, char const *argv[])
 42 {
 43     int N,M;
 44     //freopen("input.txt","r",stdin);
 45     while(scanf("%d %d",&N,&M) != EOF) {
 46         for(int i = 0; i < N; i++) {
 47             scanf("%d %d %d",&old[i].goldNum,&old[i].medalNum,&old[i].population);
 48         }
 49         for(int i = 0; i < M; i++) {
 50             int toCal;
 51             scanf("%d",&toCal);
 52             cal[i] = old[toCal];
 53             cal[i].goldRatio = (double)cal[i].goldNum/(double)cal[i].population;
 54             cal[i].medalRatio = (double)cal[i].medalNum/(double)cal[i].population;
 55             cal[i].placeGold = cal[i].placeMedal = cal[i].placeGR = cal[i].placeMR = MAX;
 56             cal[i].num = i;
 57         }
 58         qsort(cal,M,sizeof(country),compareGOLD);
 59         
 60         
 61         
 62         int place = 1;
 63         cal[0].placeGold = 1;
 64         for(int i = 1; i < M; i++) {
 65             if(cal[i].goldNum != cal[i-1].goldNum) {
 66                 place = i+1;
 67             }
 68             
 69             cal[i].placeGold = place;
 70         }
 71         
 72         /*for(int i = 0; i < M; i++) {
 73             
 74             printf("%d %d %d\n",cal[i].num,cal[i].goldNum,cal[i].placeGold);
 75         }
 76         printf("\n");*/    
 77         
 78         qsort(cal,M,sizeof(country),compareMEDAL);
 79         place = 1;
 80         cal[0].placeMedal = 1;
 81         for(int i = 1; i < M; i++) {
 82             if(cal[i].medalNum != cal[i-1].medalNum) {
 83                 place = i+1;
 84             }
 85             
 86             cal[i].placeMedal = place;
 87         }
 88 
 89         qsort(cal,M,sizeof(country),comparegoldRatio);
 90         place = 1;
 91         cal[0].placeGR = 1;
 92         for(int i = 1; i < M; i++) {
 93             if(cal[i].goldRatio != cal[i-1].goldRatio) {
 94                 place = i+1;
 95             }
 96         
 97             cal[i].placeGR = place;
 98         }
 99 
100         qsort(cal,M,sizeof(country),comparemedalRatio);
101         place = 1;
102         cal[0].placeMR = 1;
103         for(int i = 1; i < M; i++) {
104             if(cal[i].placeMR != cal[i-1].placeMR) {
105                 place = i+1;
106             }
107             
108             cal[i].placeMR = place;
109         }
110 
111         qsort(cal,M,sizeof(country),compareNum);
112         for(int i = 0; i < M; i++) {
113             int min = cal[i].placeGold;
114             int way = BASIC;
115             if(cal[i].placeMedal < min) {
116                 min = cal[i].placeMedal;
117                 way = BASIC + 1;
118             }
119             if(cal[i].placeGR< min) {
120                 min = cal[i].placeGR;
121                 way = BASIC + 2;
122             }
123             if(cal[i].placeMR< min) {
124                 min = cal[i].placeMR;
125                 way = BASIC + 3;
126             }
127             printf("%d:%d\n",min,way);
128         }
129         printf("\n");
130         
131         /*printf("M is %d\n",M);
132         for(int i = 0; i < M; i++) {
133             
134             printf("%d %d %d\n",cal[i].num,cal[i].goldNum,cal[i].placeGold);
135         }
136         printf("\n");*/
137     }    
138     return 0;
139 }

多么痛的领悟!

九度oj 题目1007:奥运排序问题

标签:

原文地址:http://www.cnblogs.com/jasonJie/p/5656618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!