1025 PAT Ranking

1025 PAT Ranking

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

VS2022和PAT平台的编译器不同,代码需要微调

题目要求最后的输出内容为每个用户的注册号码、最终排名、考场号和考场内部排名

由于最后需要将所有用户进行统一的排序并进行输出,所以在student结构体中不需要有最终排名的内容

#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;


struct Student
{
  char id[15];
  int score;
  int num_of_loc; //考场号
  int rank_of_loc;  //考场内部排名
};

struct Student stu[30010];


bool cmp(Student a, Student b)
{
  //为sort函数准备参数
  if (a.score != b.score)
    return a.score > b.score;
  else
    return strcmp(a.id , b.id)<0;
}

int main()
{
  int N = 0;  //考场数
  int k = 0;  //某考场内部的考生数
  int num = 0;  //考生总数
  scanf_s("%d", &N);

  for (int i = 0; i < N; i++)
  {
    //每个考场单独进行处理并输入stu数组中
    scanf_s("%d", &k);
    for (int j = 0; j < k; j++)
    {
      scanf_s("%s %d", &stu[num].id, sizeof(char) * 14, &stu[num].score);
      stu[num].num_of_loc = i+1;
      num++;
    }

    sort(stu + num - k, stu + num, cmp);
    stu[num - k].rank_of_loc = 1;
    for (int r = num - k + 1; r < num; r++)
    {
      //考场内部排名
      if (stu[r].score != stu[r - 1].score)
      {
        stu[r].rank_of_loc = r+1-(num-k);
      }
      else
      {
        stu[r].rank_of_loc = stu[r - 1].rank_of_loc;
      }
    }    
  }

  printf("%d\n", num);
  sort(stu, stu + num, cmp);
  int r = 1; //总排名
  for (int i = 0; i < num; i++)
  {
    if (i > 0 && stu[i].score != stu[i - 1].score)
    {
      r=i+1;
    }
    printf("%s ", stu[i].id);
    printf("%d %d %d\n", r, stu[i].num_of_loc, stu[i].rank_of_loc);
  }
  printf("\n");
}

在解题时要抛弃题目给什么条件就用什么条件的习惯,很多题目都可以利用一个大数组来简化做法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值