当前位置: 代码迷 >> 综合 >> [PAT A1025]PAT Ranking
  详细解决方案

[PAT A1025]PAT Ranking

热度:24   发布时间:2023-12-15 06:29:35.0

[PAT A1025]PAT Ranking

题目描述

1025 PAT Ranking (25 分)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.

输入格式

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.

输出格式

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.

输入样例

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

输出样例

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

分析

题目大意

就是输入考场数N,然后对于每个考场,输入考场内考生数目K,接下来K条输入是输入学生的准考证号和考试成绩,要求是成绩从高到低排序,对于相同的成绩按照准考证号从低到高排序

算法分析

  1. 其实题意很简单,就是考察简单的排序,我们考虑使用 sort ()函数来简化计算,当然,我们必须重写cmp函数!
  2. 然后对于空间的预分配,因为N<=100,K<=300,NK<=30000,所以数组大小为30010就行

具体代码如下:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct student
{string r_number;//准考证号int l_number;  //考场号int l_rank = 0; //考场排名int grade;  //考试成绩
}; //结构体的设计
struct student stu[30010];  //数组的大小为30010,太小有可能通不过
bool cmp(student a, student b)
{if (a.grade != b.grade) return a.grade > b.grade;  //a,b学生成绩不相等,那么按降序排列else return a.r_number < b.r_number;  //a,b学生成绩相等,按准考证号升序排列
}
int main()
{int N, K, SUM = 0, R = 0;   //R用来计数cin >> N;for (int i = 0; i < N; i++){cin >> K;for (int j = 0; j < K; j++){student s;cin >> s.r_number >> s.grade;s.l_number = i + 1;      //考场号按照升序自动升,第一个输入的就是1考场,第二个输入就是2考场stu[R++] = s;}sort(stu + SUM, stu + SUM + K, cmp);stu[SUM].l_rank = 1;for (int i = SUM + 1; i < SUM + K; i++){if (stu[i].grade == stu[i - 1].grade) stu[i].l_rank = stu[i - 1].l_rank;else stu[i].l_rank = i - SUM + 1;}SUM += K;}sort(stu, stu + SUM, cmp);int r = 1;cout << SUM << endl;for (int i = 0; i < SUM; i++){if (i>0 && stu[i].grade != stu[i - 1].grade){r = i + 1;}cout << stu[i].r_number << " " << r << " " << stu[i].l_number << " " << stu[i].l_rank << endl;}return 0;
}

编程中有可能遇到的小问题(必看!可能你代码过不了是因为它!)

  1. 审题问题:由于英文题目,第一次写的时候看的太粗略,没看见如果分数相同则按照准考证号升序排列
  2. 内存问题:由于数组的大小是30010,非常大,我记得好像在main函数里面定义使用的是系统栈,可能导致溢出(我就遇到了),所以应该定义为全局变量(划重点!!!

水平有限,如果代码有任何问题或者有不明白的地方,欢迎在留言区评论;也欢迎各位大牛提出宝贵的意见!

  相关解决方案