PTA數據結構與算法題目集(中文) 7-40奧運排行榜 (25 分)


PTA數據結構與算法題目集(中文)  7-40奧運排行榜 (25 分)

7-40 奧運排行榜 (25 分)
 

每年奧運會各大媒體都會公布一個排行榜,但是細心的讀者發現,不同國家的排行榜略有不同。比如中國金牌總數列第一的時候,中國媒體就公布“金牌榜”;而美國的獎牌總數第一,於是美國媒體就公布“獎牌榜”。如果人口少的國家公布一個“國民人均獎牌榜”,說不定非洲的國家會成為榜魁…… 現在就請你寫一個程序,對每個前來咨詢的國家按照對其最有利的方式計算它的排名。

輸入格式:

輸入的第一行給出兩個正整數N和M(≤,因為世界上共有224個國家和地區),分別是參與排名的國家和地區的總個數、以及前來咨詢的國家的個數。為簡單起見,我們把國家從0 ~ N1編號。之后有N行輸入,第i行給出編號為i1的國家的金牌數、獎牌數、國民人口數(單位為百萬),數字均為[0,1000]區間內的整數,用空格分隔。最后面一行給出M個前來咨詢的國家的編號,用空格分隔。

輸出格式:

在一行里順序輸出前來咨詢的國家的排名:計算方式編號。其排名按照對該國家最有利的方式計算;計算方式編號為:金牌榜=1,獎牌榜=2,國民人均金牌榜=3,國民人均獎牌榜=4。輸出間以空格分隔,輸出結尾不能有多余空格。

若某國在不同排名方式下有相同名次,則輸出編號最小的計算方式。

輸入樣例:

4 4
51 100 1000
36 110 300
6 14 32
5 18 40
0 1 2 3

輸出樣例:

1:1 1:2 1:3 1:4
題目分析:利用表排序和快速排序對每個元素進行排序並寫入到結構體數組中,注意當金牌數相同時,后一個排名與上一個相同
題外話:我自己寫的比較相同排名時的函數過不了--來自菜雞的抱怨
 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<malloc.h>
 5 
 6 struct Country
 7 {
 8     float Medals[4];
 9     int Sort[4];
10 }Countrys[224];
11 int Table[224];
12 int Note[224];
13 void Swap(int i, int j)
14 {
15     int tmp = Table[i];
16     Table[i] = Table[j];
17     Table[j] = tmp;
18 }
19 void InitializeTable(int N)
20 {
21     for (int i = 0; i < N; i++)
22     {
23         Table[i] = i;
24         Note[i] = 0;
25     }
26 }
27 void QuickSort(int start,int end,int i)
28 {
29     if (start >= end - 1)
30         return;
31     int mid = (start + end) / 2;
32     Swap(start, mid);
33     int k = start + 1;
34     for (int j = start + 1; j < end; j++)
35     {
36         if (Countrys[Table[j]].Medals[i] > Countrys[Table[start]].Medals[i])
37             Swap(k++, j);
38     }
39     Swap(start, k- 1);
40     QuickSort(start, k - 1, i);
41     QuickSort(k, end, i);
42 }
43 void Judget(int N,int i)
44 {
45     for (int j = 0; j < N; j++)
46     {
47         if (j > 0 && Countrys[Table[j]].Medals[i] == Countrys[Table[j - 1]].Medals[i])
48             Countrys[Table[j]].Sort[i] = Countrys[Table[j - 1]].Sort[i];
49         else
50             Countrys[Table[j]].Sort[i] = j;
51     }
52 }
53 int main()
54 {
55     int N, M;
56     scanf("%d%d", &N, &M);
57     float num;
58     for (int i = 0; i < N; i++)
59     {
60         scanf("%f%f%f", &Countrys[i].Medals[0], &Countrys[i].Medals[1], &num);
61         Countrys[i].Medals[2] = Countrys[i].Medals[0] / num;
62         Countrys[i].Medals[3] = Countrys[i].Medals[1] / num;
63     }
64     for (int i = 0; i < 4; i++)
65     {
66         InitializeTable(N);
67         QuickSort(0, N, i);
68         Judget(N,i);
69     }
70     int n;
71     for (int i = 0; i < M; i++)
72     {
73         int MinSort = 65535;
74         int Min = 65535;
75         scanf("%d", &n);
76         for (int j = 0; j < 4; j++)
77         {
78             if (Countrys[n].Sort[j] < MinSort)
79             {
80                 MinSort = Countrys[n].Sort[j];
81                 Min = j;
82             }
83             else if (Countrys[n].Sort[j] == MinSort && j < Min)
84                 Min = j;
85         }
86         if(i!=M-1)
87             printf("%d:%d ", MinSort+1, Min+1);
88         else
89             printf("%d:%d", MinSort + 1, Min + 1);
90     }
91     return 0;
92 }
View Code

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM