各位同志们,这道题俺郁闷了好久了。在网上找了可以AC的代码测试了好几组数据,都正确。可是提交老是Wrong Answer. 请同志们路过的时候歇歇脚,帮我看看。鄙人感激不尽。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct Medal
{
int number;
int gold_medal;
int all_medal;
int population;
}value[1000];
int cmp1(const void *value1, const void *value2)
{
struct Medal *p = (struct Medal *)value2;
struct Medal *q = (struct Medal *)value1;
return p ->gold_medal - q->gold_medal;
}
int cmp2(const void *value1, const void *value2)
{
struct Medal *p = (struct Medal *)value2;
struct Medal *q = (struct Medal *)value1;
return p ->all_medal - q->all_medal;
}
void swap(struct Medal *s,int i,int j)
{
int temp;
temp = value[i].number;
value[i].number = value[j].number;
value[j].number = temp;
temp = value[i].gold_medal;
value[i].gold_medal = value[j].gold_medal;
value[j].gold_medal = temp;
temp = value[i].all_medal;
value[i].all_medal = value[j].all_medal;
value[j].all_medal = temp;
temp = value[i].population;
value[i].population = value[j].population;
value[j].population = temp;
}
void sort3(struct Medal *s,int left,int right)
{
int i,j,temp;
for(i = 0; i < right; i ++)
{
temp = i;
for(j = i + 1; j <= right; j ++)
{
if(value[temp].gold_medal * value[j].population < value[j].gold_medal * value[temp].population)
{
temp = j;
}
}
if(temp != i)
{
swap(value,i,temp);
}
}
}
void sort4(struct Medal *s,int left,int right)
{
int i,j,temp;
for(i = 0; i < right; i ++)
{
temp = i;
for(j = i + 1; j <= right; j ++)
{
if(value[temp].all_medal * value[j].population < value[j].all_medal * value[temp].population)
{
temp = j;
}
}
if(temp != i)
{
swap(value,i,temp);
}
}
}
int main()
{
int country,sort_country,i,j;
int rank[1000][2],rank1[1000];
int country_number[1000];
while(scanf("%d%d",&country,&sort_country) == 2)
{
for(i = 0; i < country; i ++)
{
value[i].number = i;
scanf("%d%d%d",&value[i].gold_medal,&value[i].all_medal,&value[i].population);
}
for(i = 0; i < sort_country; i ++)
{
scanf("%d",&country_number[i]);
if(country_number[i] != value[i].number)
{
swap(value,i,country_number[i]);
}
}
qsort(value,sort_country,sizeof(struct Medal),cmp1);
rank1[value[0].number] = 1;
for(i = 1; i < sort_country; i ++)
{
if(value[i].gold_medal == value[i - 1].gold_medal)
{
rank1[value[i].number] = rank1[value[i - 1].number];
}
else
{
rank1[value[i].number] = i + 1;
}
}
for(j = 0; j < sort_country; j ++)
{
for(i = 0; i < sort_country; i ++)
{
if(value[i].number == country_number[j])
{
rank[j][0] = rank1[value[i].number];
rank[j][1] = 1;
}
}
}
qsort(value,sort_country,sizeof(struct Medal),cmp2);
rank1[value[0].number] = 1;
for(i = 1; i < sort_country; i ++)
{
if(value[i].all_medal == value[i - 1].all_medal)
{
rank1[value[i].number] = rank1[value[i - 1].number];
}
else
{
rank1[value[i].number] = i + 1;
}
}
for(j = 0; j < sort_country; j ++)
{
for(i = 0; i < sort_country; i ++)
{
if(value[i].number == country_number[j] && rank[j][0] > rank1[value[i].number] )
{
rank[j][0] = rank1[value[i].number];
rank[j][1] = 2;
}
}
}
sort3(value,0,sort_country - 1);
rank1[value[0].number] = 1;
for(i = 1; i < sort_country; i ++)
{
if(value[i].gold_medal * value[i - 1].population == value[i - 1].gold_medal * value[i].population)
{
rank1[value[i].number] = rank1[value[i - 1].number];
}
else
{
rank1[value[i].number] = i + 1;
}
}
for(j = 0; j < sort_country; j ++)
{
for(i = 0; i < sort_country; i ++)
{
if(value[i].number == country_number[j] && rank[j][0] > rank1[value[i].number] )
{
rank[j][0] = rank1[value[i].number];
rank[j][1] = 3;
}
}
}
sort4(value,0,sort_country - 1);
rank1[value[0].number] = 1;
for(i = 1; i < sort_country; i ++)
{
if(value[i].all_medal * value[i - 1].population == value[i - 1].all_medal * value[i].population)
{
rank1[value[i].number] = rank1[value[i - 1].number];
}
else
{
rank1[value[i].number] = i + 1;
}
}
for(j = 0; j < sort_country; j ++)
{
for(i = 0; i < sort_country; i ++)
{
if(value[i].number == country_number[j] && rank[j][0] > rank1[value[i].number] )
{
rank[j][0] = rank1[value[i].number];
rank[j][1] = 4;
}
}
}
for(i = 0; i < sort_country; i ++)
{
printf("%d:%d\n",rank[i][0],rank[i][1]);
}
printf("\n");
}
return 0;
}
如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4.