数学趣题——选美比赛

   1: #include "stdio.h"
   2:  
   3: struct player {
   4:     int num;
   5:     int score;
   6:     int rand;
   7: } ;
   8:  
   9: // 根据对选手的得分排序
  10: void  sortScore(struct player psn[], int n)
  11: {
  12:     int i, j;
  13:     struct player tmp;
  14:  
  15:     for(i = 0; i < n - 1; i++)
  16:         for(j = 0; j < n - 1 - i; j++)
  17:         {
  18:             if(psn[j].score > psn[j+1].score)
  19:             {
  20:                 tmp = psn[j];
  21:                 psn[j] = psn[j+1];
  22:                 psn[j+1] = tmp;
  23:             }
  24:         }
  25: }
  26:  
  27: // 对已经根据得分排过序的选手,进行排名赋值
  28: void setRand(struct player psn[], int n)
  29: {
  30:     int i, j = 2;
  31:     psn[0].rand = 1;
  32:  
  33:     for(i = 1; i < n; i++)
  34:     {
  35:         if(psn[i].score != psn[i-1].score )
  36:         {
  37:             psn[i].rand = j;
  38:             j++;
  39:         }
  40:         else
  41:             psn[i].rand = psn[i-1].rand;
  42:  
  43:     }
  44: }
  45:  
  46: // 再按选手序号进行排序
  47: void  sortNum(struct player psn[], int n)
  48: {
  49:     int i, j;
  50:     struct player tmp;
  51:  
  52:     for(i = 0; i < n - 1; i++)
  53:         for(j = 0; j < n - 1 - i; j++)
  54:         {
  55:             if(psn[j].num > psn[j+1].num)
  56:             {
  57:                 tmp = psn[j];
  58:                 psn[j] = psn[j+1];
  59:                 psn[j+1] = tmp;
  60:             }
  61:         }
  62: }
  63:  
  64: void sortRand(struct player psn[], int n)
  65: {
  66:     sortScore(psn, n);         /*以分数为关键字排序*/
  67:     setRand(psn, n);          /*按照分数排名次*/
  68:     sortNum(psn, n);         /*按照序号重新排序*/
  69: }
  70:  
  71: int main()
  72: {
  73:     struct player psn[7] = {{1, 5, 0}, {2, 3, 0}, {3, 4, 0}, {4, 7, 0}, {5, 3, 0}, {6, 5, 0}, {7, 6, 0}};
  74:     int i;
  75:     sortRand(psn, 7);
  76:     printf("num   score rand  \n");
  77:  
  78:     for(i = 0; i < 7; i++)
  79:     {
  80:         printf("%d%6d%6d\n", psn[i].num, psn[i].score, psn[i].rand);
  81:     }
  82:  
  83:     return 0;
  84: }
  85:  

你可能感兴趣的:(数学)