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:

 1 2
 2 5
 3 1234567890001 95
 4 1234567890005 100
 5 1234567890003 95
 6 1234567890002 77
 7 1234567890004 85
 8 4
 9 1234567890013 65
10 1234567890011 25
11 1234567890014 100
12 1234567890012 85

Sample Output:

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

关键点:

1.结构体的构造和写入;

2.排序函数的应用和cmp函数的定义;

3.先排考场内排名,后排所有人的名次,以及不同名次的记录;

4.如何在结构体数组中找到不同考场的人。

代码如下:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 struct Student
 6 {
 7     char id[15];
 8     int score;
 9     int location_number;
10     int local_rank;
11 }stu[30010];
12 bool cmp(Student a, Student b)
13 {
14     if(a.score != b.score)
15     {
16         return a.score > b.score;
17     }
18     else
19     {
20         return strcmp(a.id, b.id) < 0;
21     }
22 }
23 int main()
24 {
25     int n, k, num = 0;
26     scanf("%d", &n);
27     for(int i = 1; i <= n; i++)
28     {
29         scanf("%d", &k);
30         for(int j = 0; j < k; j++)
31         {
32             scanf("%s %d", stu[num].id, &stu[num].score);
33             stu[num].location_number = i;
34             num++;
35         }
36         sort(stu+num-k, stu+num, cmp);
37         stu[num-k].local_rank = 1;
38         for(int j = 1; j < k; j++)
39         {
40             if(stu[num-k+j].score == stu[num-k+j-1].score)
41             {
42                 stu[num-k+j].local_rank = stu[num-k+j-1].local_rank;
43             }
44             else
45             {
46                 stu[num-k+j].local_rank = j + 1;
47             }
48         }
49     }
50     printf("%d\n", num);
51     sort(stu, stu+num, cmp);
52     int r = 1;//当前考生的排名
53     for(int i = 0; i < num; i++)
54     {
55         if(i>0 && stu[i].score != stu[i-1].score)
56         {
57             r = i + 1;
58         }
59         printf("%s ", stu[i].id);
60         printf("%d %d %d\n", r, stu[i].location_number, stu[i].local_rank);
61     }
62     return 0;
63 }

 

 

 

posted on 2019-07-21 21:01  Narnianemo  阅读(235)  评论(0编辑  收藏  举报