在做题过程中
要注意:
比率是按照浮点数形式表示的
#include <stdio.h>
#include <stdlib.h>
typedef struct Nation
{
int GoldMedalNum;
int MedalNum;
int Population;
float GPRatio;
float MPRatio; //这里一定要用比值,否则出错
} Nation;
int main()
{
int N, M; // N是国家数,M是排名的国家数
while (scanf("%d%d", &N, &M) != EOF)
{
Nation Nat[N];
for (int i = 0; i < N; i++)
{
scanf("%d%d%d",
&Nat[i].GoldMedalNum,
&Nat[i].MedalNum,
&Nat[i].Population);
Nat[i].GPRatio = Nat[i].GoldMedalNum ? (float)(Nat[i].GoldMedalNum) / (float)(Nat[i].Population) : 0;
Nat[i].MPRatio = Nat[i].MedalNum ? (float)(Nat[i].MedalNum) / (float)(Nat[i].Population) : 0;
//这里的除法也要用float转换
}
int ToBeRank[M][5];
for (int i = 0; i < M; i++)
for (int j = 0; j < 5; j++)
ToBeRank[i][j] = 1; //令其初始排名均为1
for (int i = 0; i < M; i++)
scanf("%d", &ToBeRank[i][0]); //第零行为参加排名的国家号
//以上即录入了所有的信息
//对于这种,排名需要1,2,2,4的,需要对于每一个元素遍历一次
//遍历过程中,发现比自己排名高的,则自增一位,相同,或者比自己低的不变
for (int i = 0; i < M; i++)
for (int j = 0; j < M; j++)
{
if (Nat[ToBeRank[i][0]].GoldMedalNum < Nat[ToBeRank[j][0]].GoldMedalNum)
ToBeRank[i][1]++;
if (Nat[ToBeRank[i][0]].MedalNum < Nat[ToBeRank[j][0]].MedalNum)
ToBeRank[i][2]++;
if (Nat[ToBeRank[i][0]].GPRatio < Nat[ToBeRank[j][0]].GPRatio)
ToBeRank[i][3]++;
if (Nat[ToBeRank[i][0]].MPRatio < Nat[ToBeRank[j][0]].MPRatio)
ToBeRank[i][4]++;
}
//接下来输出排名
for (int i = 0; i < M; i++)
{
int MaxRank = ToBeRank[i][1], RankMethod = 1;
for (int j = 2; j <= 4; j++)
if (MaxRank > ToBeRank[i][j])
MaxRank = ToBeRank[i][j], RankMethod = j;
printf("%d:%d\n", MaxRank, RankMethod);
}
printf("\n");
}
return 0;
}