PAT甲级真题 A1025 PAT Ranking
题目概述: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: 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.
Sample Input:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
Sample Output:
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个学生,依次输入这些学生的考号与成绩,并按照成绩由高到低(如果成绩相同则按字典序由低到高)排序,并依次输出该学生成绩的总排名,考场号,以及在考场的排名。
题解:使用结构体储存学生信息,我的结构体共有5个变量,当然也有用4个的
struct student
{
char number[105];
int countnumber;
int lanknumber;
int all;
int score;
};
大致思路为使用两次sort排序,先在本考场内进行一次排序,得出学生在考场的位序,其次在全部学生进行一次排序。cmp构造如下:
bool cmp(student a, student b)
{
if (a.score != b.score)
return a.score > b.score;
if (a.score == b.score)
return strcmp(a.number, b.number) < 0;
}
注意在PTA上提交时要加上cstring头文件,否则会编译错误
AC代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include <cstring>
using namespace std;
struct student
{
char number[105];
int countnumber;
int lanknumber;
int all;
int score;
};
bool cmp(student a, student b)
{
if (a.score != b.score)
return a.score > b.score;
if (a.score == b.score)
return strcmp(a.number, b.number) < 0;
}
int main()
{
struct student box[30005];
int count, m = 0, n = 0, now1 = 1, now2 = 1;
int everycount[105];
cin >> count;
for (int t = 0; t < count; t++)
{
cin >> everycount[m];
for (int t1 = 0; t1 < everycount[m]; t1++)
{
cin >> box[n].number >> box[n].score;
box[n].countnumber = m+1;
n++;
}
sort(box + n - everycount[m], box + n, cmp);
now1 = 1, now2 = 1;
for (int t3 = n - everycount[m]; t3 < n; t3++)
{
if (t3 != 0 && box[t3 - 1].score == box[t3].score)
box[t3].lanknumber = box[t3 - 1].lanknumber;
else
box[t3].lanknumber = now2;
now2++;
}
m++;
}
sort(box, box + n, cmp);
now1 = 1, now2 = 1;
for (int t3 = 0; t3 < n; t3++)
{
if (t3 != 0 && box[t3 - 1].score