杭电ACM 3789 奥运排序问题

其实这题比较简单,主要是考排序算法和比较的过程,只要细心就好了,特别是看懂题目。

很容易就会给所有的国家排名,其实只要给要求排名的国家排名就好了。

我的思路是 根据四种排名方式,选用qsort()给要求排名的国家排序,然后各自榜排名的结果与上次排名相比较,

有更加好的排名就替换上次一的排名。这样排完所有的方式,此题得解。

我在网上还看到其他的思路,不一次次排,而是统计要求排名的国家根据四种排名方式所得的名次,再分别确定各个国家的

最终排名和排名方式。

此题的URL http://acm.hdu.edu.cn/showproblem.php?pid=3789

我的代码必较长,不简洁。那个swap()也可用结构体直接复制实现。

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

struct Medal
{
 int number;
 int gold_medal;
 int all_medal;
 int population;
 float gold_pop;
 float all_pop;
}value[1000],valueall[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;
}
int cmp3(const void *value1, const void *value2)
{
 struct Medal *p = (struct Medal *)value2;
 struct Medal *q = (struct Medal *)value1;
 return p->gold_pop > q->gold_pop ? 1 : -1;
}
int cmp4(const void *value1, const void *value2)
{
 struct Medal *p = (struct Medal *)value2;
 struct Medal *q = (struct Medal *)value1;
 return p->all_pop > q->all_pop ? 1 : -1;
}

void swap(struct Medal *s,int i,int j)
{
 int temp;
 float temp1;
 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;

 temp1 = value[i].all_pop;
 value[i].all_pop = value[j].all_pop;
 value[j].all_pop = temp1;

 temp1 = value[i].gold_pop;
 value[i].gold_pop = value[j].gold_pop;
 value[j].gold_pop = temp1;
}

int main()
{
 int country,sort_country,i,j;
 int rank[1000][2],rank1[1000];//rank 要求排名的各国家总的排名情况,包括排名与排名方式。rank1 要求排名的各国家以当前排名方式所得的名次
 int country_number[1000];
 while(scanf("%d%d",&country,&sort_country) == 2)
 {
  for(i = 0; i < country; i ++)
  {
   valueall[i].number = i;
   scanf("%d%d%d",&valueall[i].gold_medal,&valueall[i].all_medal,&valueall[i].population);
   valueall[i].gold_pop = (float)valueall[i].gold_medal / (float)valueall[i].population;
   valueall[i].all_pop = (float)valueall[i].all_medal / (float)valueall[i].population;
  }
  for(i = 0; i < sort_country; i ++) //把要求排名的国家放到一起
  {
   scanf("%d",&country_number[i]);
   value[i] = valueall[country_number[i]];
  }
  qsort(value,sort_country,sizeof(struct Medal),cmp1);
  rank1[value[0].number] = 1;
  for(i = 1; i < sort_country; i ++) //确定以金牌数为准的国家排名,rank1的下标是国家的序号
  {
   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;
    }
   }
  }
  qsort(value,sort_country,sizeof(struct Medal),cmp3);
  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;
    }
   }
  }
  qsort(value,sort_country,sizeof(struct Medal),cmp4);
  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;
}

posted @ 2011-06-16 12:56  ProgrammingEveryday  阅读(294)  评论(0编辑  收藏  举报