标签:
题目描述:
按要求,给国家进行排名。
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;