PTA 乙级 1085 PAT单位排行

论坛 期权论坛 脚本     
匿名技术用户   2020-12-27 08:23   11   0

代码实现:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct
{
 char school[10]; int B, A, T, student, sum, rank;
}score;

int cmp_school(const void *a, const void *b)
{
 score *aa = (score*)a, *bb = (score*)b;
 return strcmp(aa->school, bb->school);
}

int cmp(const void *a, const void *b)
{
 score *aa = (score*)a, *bb = (score*)b;
 if (aa->sum != bb->sum)
  return aa->sum > bb->sum ? -1 : 1;
 else if (aa->sum == bb->sum&&aa->student != bb->student)
  return aa->student > bb->student ? 1 : -1;
 else if (aa->sum == bb->sum&&aa->student == bb->student)
  return strcmp(aa->school, bb->school);
}

int main()
{
 int N, cnt = 0;
 score *array;

 scanf("%d", &N);
 array = (score*)malloc(sizeof(score) * N);
 for (int i = 0; i < N; i++)
  array[i].B = array[i].A = array[i].T = array[i].student = 0;
 for (int i = 0; i < N; i++)
 {
  char number[10], school[10];
  int score, num, j;
  scanf("%s %d %s", number, &score, school);
  for (j = 0; school[j] != 0; j++)
  {
   if (school[j] >= 'A'&&school[j] <= 'Z')
    school[j] += 32;
   array[i].school[j] = school[j];
  }
  array[i].school[j] = 0;
  if (number[0] == 'B')
   array[i].B += score;
  else if (number[0] == 'A')
   array[i].A += score;
  else array[i].T += score;
 }
 qsort(array, N, sizeof(score), cmp_school);
 for (int i = 0; i < N - 1; i++)
  if (strcmp(array[i].school, array[i + 1].school) == 0)
  {
   int j;
   for (j = i + 1; strcmp(array[i].school, array[j].school) == 0; j++)
    array[i].B += array[j].B, array[i].A += array[j].A,
    array[i].T += array[j].T, array[i].student++, array[j].school[0] = 0;
   i = j - 1;
  }

 score *school = (score*)malloc(sizeof(score)*N);
 for (int i = 0; i < N; i++)
  if (array[i].school[0] != 0)
   school[cnt++] = array[i];
 for (int i = 0; i < cnt; i++)
  school[i].sum = (double)school[i].B * 2 / 3 + (double)school[i].A + (double)school[i].T * 3 / 2;
 qsort(school, cnt, sizeof(score), cmp);
 for (int i = 0; i < cnt; i++)
  school[i].rank = i + 1;
 for (int i = 1; i < cnt; i++)
  if (school[i].sum == school[i - 1].sum)
   school[i].rank = school[i - 1].rank;
 printf("%d\n", cnt);
 for (int i = 0; i < cnt; i++)
  printf("%d %s %d %d\n", school[i].rank, school[i].school, school[i].sum, school[i].student + 1);

 return 0;
}
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP