1025 PAT Ranking(排序)

阳光穿透心脏的1/2处 2024-04-08 09:56 197阅读 0赞

1025 PAT Ranking

0、题目

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

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:

  1. 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.

1、大致题意

n 个考场,每个考场有若干数量的学生,给出每个考场中考生的编号和分数,要求算排名,输出所有考生的编号、排名、考场号、考场内排名

2、基本思路

简单题

3、解题过程

3.1 坑点 - 测试点4(22/25)

ID有类似 000003 这种形式的,如果是数字类型就容易输出错误,前导零别忘了。

  1. #include<iostream>
  2. #include<algorithm>
  3. int n,k[105],cnt;
  4. struct Student {
  5. long long num;
  6. int score;
  7. int final_rank,location_number,local_rank;
  8. bool operator >(const Student &a)const {
  9. if(score!=a.score) {
  10. return score>a.score;
  11. } else {
  12. return num<a.num;
  13. }
  14. };
  15. } stu[10000005];
  16. using namespace std;
  17. int main() {
  18. cin>>n;
  19. cnt=0;
  20. for(int i=1; i<=n; i++) {
  21. cin>>k[i];
  22. for(int j=1; j<=k[i]; j++) {
  23. cin>>stu[cnt+j].num>>stu[cnt+j].score;
  24. }
  25. sort(stu+cnt+1,stu+cnt+k[i]+1,greater<Student>());
  26. stu[cnt+1].location_number=i;
  27. stu[cnt+1].local_rank=1;
  28. for(int j=2; j<=k[i]; j++) {
  29. if(stu[cnt+j].score==stu[cnt+j-1].score) {
  30. stu[cnt+j].local_rank=stu[cnt+j-1].local_rank;
  31. } else {
  32. stu[cnt+j].local_rank=j;
  33. }
  34. stu[cnt+j].location_number=i;
  35. }
  36. cnt+=k[i];
  37. }
  38. sort(stu+1,stu+cnt+1,greater<Student>());
  39. stu[1].final_rank=1;
  40. for(int i=2; i<=cnt; i++) {
  41. if(stu[i].score==stu[i-1].score) {
  42. stu[i].final_rank=stu[i-1].final_rank;
  43. } else {
  44. stu[i].final_rank=i;
  45. }
  46. }
  47. cout<<cnt<<endl;
  48. // 1234567890005 1 1 1
  49. for(int i=1; i<=cnt; i++) {
  50. cout<<stu[i].num<<" "<<stu[i].final_rank<<" "<<stu[i].location_number<<" "<<stu[i].local_rank<<endl;
  51. }
  52. return 0;
  53. }

在这里插入图片描述

3.2 AC代码

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<iomanip>
  4. int n,k[105],cnt;
  5. struct Student {
  6. long long num;
  7. int score;
  8. int final_rank,location_number,local_rank;
  9. bool operator >(const Student &a)const {
  10. if(score!=a.score) {
  11. return score>a.score;
  12. } else {
  13. return num<a.num;
  14. }
  15. };
  16. } stu[10000005];
  17. using namespace std;
  18. int main() {
  19. cin>>n;
  20. cnt=0;
  21. for(int i=1; i<=n; i++) {
  22. cin>>k[i];
  23. for(int j=1; j<=k[i]; j++) {
  24. cin>>stu[cnt+j].num>>stu[cnt+j].score;
  25. }
  26. sort(stu+cnt+1,stu+cnt+k[i]+1,greater<Student>());
  27. stu[cnt+1].location_number=i;
  28. stu[cnt+1].local_rank=1;
  29. for(int j=2; j<=k[i]; j++) {
  30. if(stu[cnt+j].score==stu[cnt+j-1].score) {
  31. stu[cnt+j].local_rank=stu[cnt+j-1].local_rank;
  32. } else {
  33. stu[cnt+j].local_rank=j;
  34. }
  35. stu[cnt+j].location_number=i;
  36. }
  37. cnt+=k[i];
  38. }
  39. sort(stu+1,stu+cnt+1,greater<Student>());
  40. stu[1].final_rank=1;
  41. for(int i=2; i<=cnt; i++) {
  42. if(stu[i].score==stu[i-1].score) {
  43. stu[i].final_rank=stu[i-1].final_rank;
  44. } else {
  45. stu[i].final_rank=i;
  46. }
  47. }
  48. cout<<cnt<<endl;
  49. for(int i=1; i<=cnt; i++) {
  50. cout<<setfill('0')<<setw(13)<<stu[i].num<<" "<<stu[i].final_rank<<" "<<stu[i].location_number<<" "<<stu[i].local_rank<<endl;
  51. }
  52. return 0;
  53. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,197人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PAT乙级1025

    1025 反转链表 (25 分) 给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转。例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出

    相关 PATA1025PAT Ranking

    需要注意的就是sort函数的应用,还有自己比较函数cmp的编写 在一个就是结构体的设计,排序时的考室内的排序,数组下标的处理 参考代码: