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,在分别给出每个考场的人数和学生数据。所给的学生数据包括学号,成绩,要求按照总排名的顺序输出,每个人的总排名考场号,并且输出学生在考场的排名。
#include
#include
#include
#include
#include
using namespace std;
struct student {
string id;
int score;
int local_rank = 0;
int final_rank = 0;
int local;
student (string _str, int _score, int _local):id(_str), score(_score), local(_local) {}
};
bool cmp(student a, student b) {
if (a.score != b.score) {
return a.score > b.score;
}
else if(a.id != b.id) {
return a.id < b.id;
}
}
vector<student> stu;
void getRank() {
int final_rank = 0; //记录总排名的人数
int local_rank[110] = { 0 }; //记录每个班被排名的人数
int local[110]; //记录每个班前一个人的人位置;
fill(local, local + 100, -1);
for (int i = 0; i < stu.size(); i++) {
local_rank[stu[i].local]++;
final_rank++;
if (local[stu[i].local] == -1) {
stu[i].local_rank = local_rank[stu[i].local];
}
else {
if (stu[i].score == stu[local[stu[i].local]].score) {
stu[i].local_rank = stu[local[stu[i].local]].local_rank;
}
else {
stu[i].local_rank = local_rank[stu[i].local];
}
}
local[stu[i].local] = i;
if (i == 0) {
stu[i].final_rank = final_rank;
}
else {
if (stu[i].score == stu[i - 1].score) {
stu[i].final_rank = stu[i - 1].final_rank;
}
else {
stu[i].final_rank = final_rank;
}
}
}
}
int main() {
int n, k;
string str;
int temp;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &k);
for (int j = 0; j < k; j++) {
cin >> str;
scanf("%d", &temp);
stu.push_back(student(str, temp, i + 1));
}
}
sort(stu.begin(), stu.end(), cmp);
getRank();
temp = stu.size();
printf("%d\n", temp);
for (int i = 0; i < stu.size(); i++) {
cout << stu[i].id << " " ;
printf("%d %d %d\n", stu[i].final_rank, stu[i].local, stu[i].local_rank);
}
return 0;
}
这个题目还是对sort函数和cmp函数的使用,排序问题在不考虑自写排序算法的情况下难度都不高,搞清楚关键字和排序的对象还有读入输出问题就可以了。