题目链接:http://www.patest.cn/contests/pat-a-practise/1025
题目:
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.
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 85Sample 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
分析:
输入每一个班级同学的分数,给出每个同学的整体排名和班级排名,并且其中同分数的排名要相同。写好结构体和排序,可以在每次输入完班级的分数后对当前班级排好序得出名词,因此在结构体中有一个班级排名的字段。难度适中。
AC代码:
#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
struct Student{
char reg_num[13];
int score;
int group;
int global_rank;
int loc_rank;
}; //学生结构体,维护一个总排名global_rank和班级排名loc_rank
vector<Student>V;
bool cmp(Student A, Student B){
if (A.score != B.score)return A.score > B.score;
else{
int tmp = strcmp(A.reg_num, B.reg_num);
return tmp < 0;
}
}
int nums[101]; //记录每个班级的人数
int main(void){
//freopen("F://Temp/input.txt", "r", stdin);
int N;
while (scanf("%d", &N) != EOF){
for (int i = 0; i < N; i++){
scanf("%d", &nums[i]);
for (int j = 0; j < nums[i]; j++){
Student tmp;
scanf("%s%d", tmp.reg_num, &tmp.score);
tmp.group = i + 1;
V.push_back(tmp);
}
sort(V.end() - nums[i], V.end(), cmp); //对当前班级的同学排名,得到local_rank
int start = V.size() - nums[i];
int end = V.size() - 1;
V[start].loc_rank = 1;
for (int j = start + 1; j < end + 1; j++){
if (V[j].score == V[j - 1].score){ //要考虑分数相同排名相同的情况
V[j].loc_rank = V[j - 1].loc_rank;
}
else V[j].loc_rank = j - start + 1;
}
}
sort(V.begin() , V.end(), cmp); //对整体排名
V[0].global_rank = 1;
for (int j = 1; j < V.size(); j++){
if (V[j].score == V[j - 1].score){ //同样要考虑分数相同排名相同的情况
V[j].global_rank = V[j - 1].global_rank;
}
else V[j].global_rank = j + 1;
}
int sum = 0;
for (int i = 0; i < N; i++){
sum += nums[i];
}
printf("%d\n", sum);
for (int i = 0; i < V.size(); i++){
printf("%s %d %d %d\n", V[i].reg_num, V[i].global_rank, V[i].group, V[i].loc_rank);
}
}
return 0;
}
截图:
——Apie陈小旭