码迷,mamicode.com
首页 > 编程语言 > 详细

【算法学习记录-排序题】【PAT A1025】PAT Ranking

时间:2020-01-26 15:55:53      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:clu   gen   follow   you   tar   创建   cstring   positive   mes   

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个考场,每个考场有若干考生。

现给出各个考场所有考生的准考证号与分数,要求按分数从高到低分别进行①单考场内排序②总排序。

最终输出参加考试的考生数以及每个考生的①准考证号②总排名③考场号④考场内排名

 

思路:

1、排序的逻辑

①按分数从高到低排序

②分数相同时,按准考证号从小到大排序

2、所需要的信息及信息的存储

①对于单个学生student,需要对应的准考证号id,分数score,考场号location_number,考场内排名local_rank,总排名sum_rank——结构体Student,并创建一个足够大的结构体数组

②此外还需要总考场数n,总考生数num,单个考场内的考生数k——int型变量

3、排序逻辑的实现

1 bool cmp(Student a, Student b) {
2     if (a.score != b.score) {
3         return a.score > b.score;
4     } else {
5         return strcmp(a.id, b.id) < 0;
6     }
7 }

4、main()  分步分析

主要分为两个部分:

(1)给单个考场内的考生排名

①有n个考场,因此最外层要有for循环从1到n遍历每个考场

1 for (int i = 1; i <= n; i++) { 
2 }

②每个考场内有k个学生,因此内层要有for循环从0到k-1遍历每个学生

1 for (int j = 0; j < k; j++) {
2 }

③接收输入的准考证号id与分数score,按顺序存入结构体数组中

1 scanf("%s %d", stu[num].id, &stu[num].score);

④当前考场的学生遍历完后,调用排序函数sort()对当前考场内的所有考生按排序逻辑进行排序

1 sort(stu, stu + k, cmp);

⑤按④的排序结果,依次给每个考生的考场内排名local_rank赋值

1 stu[0].local_rank = 1;  //第一个考生的排名为1
2 for (int j = 1; j < k; j++) {
3     if (stu[j].score == stu[j - 1].score) {
4         stu[j].local_rank = stu[j - 1].local_rank; //分数相同则排名相同
5     } else {
6         stu[j].local_rank = j + 1; //分数不同则+1
7     }
8 }

(2)给所有的考生排名

⑥调用排序函数sort()对所有考生按排序逻辑进行排序(代码同④)

⑦按⑥的排序结果,依次给每个考生的总排名sum_rank赋值(代码同⑤)

5、踩到了第一个坑

只开一个Student类型的数组,但是要存放多个考场的考生信息,因此遍历和排序时不能简单的从下标0开始

解决:

引入新变量 总考生数num

于是可知,每次接收完一个新考场的考生数据时,此考场考生对应的下标范围是num - k ~ num

6、main()  雏形

 1 scanf("%d", &n); //考场数 
 2 for (int i = 1; i <= n; i++) { //考场号,要从1开始 
 3     scanf("%d", &k); //当前考场人数 
 4     
 5     /*录入数据*/
 6     for (int j = 0; j < k; j++) {
 7         scanf("%s %d", stu[num].id, &stu[num].score);
 8         stu[num].location_number = i;
 9         num++; //通过这个计算总考生数 
10     }
11 
12     /*对刚录入的这个考场考生进行排序并赋名次*/
13     sort(stu + num - k, stu + num, cmp); 
14     stu[num - k].local_rank = 1;
15     for (int j = num - k + 1; j < num; j++) {
16         if (stu[j].score == stu[j - 1].score) {
17             stu[j].local_rank = stu[j - 1].local_rank;
18         } else {
19             stu[j].local_rank = j + 1 - (num - k);//j为此考生之前的人数,- (num - k)为去掉前面其他考场的人数,+ 1后即得到此考生在本考场的排名
20         }
21     }
22 
23 }
24 
25 /*对所有考生进行排序并赋名次*/
26 sort(stu, stu + num, cmp); 
27 stu[0].sum_rank = 1;
28 for (int i = 1; i < num; i++) {
29     if (stu[i].score == stu[i - 1].score) {
30         stu[i].sum_rank = stu[i - 1].sum_rank;
31     } else {
32         stu[i].sum_rank = i + 1;
33     }
34 }
35     

7、优化

 

总排名sum_rank是在整个程序的最后才得到的

它之后紧跟着的就是要将sum_rank输出——额外去存储的意义不大

并且sum_rank的值只有两种情况①和前一个相同②i + 1——值的计算简单,没有其他的相关项

 

所以考虑将“计算出总排名→存入sum_rank→输出stu[i].sum_rank”简化为“计算出总排名→输出总排名”

 1 sort(stu, stu + num, cmp);
 2 int r = 1; //r即为总排名,初始为1
 3 for (int i = 0; i < num; i++) {
 4     //i > 0:从第二个考生开始(stu[0]为第一个考生)
 5     //stu[i].score != stu[i - 1].score:
 6     //若两人分数相同,则总排名r相同;
 7     //若不同,则后一个人的排名为其前面的人数 i + 1;
 8     if (i > 0 && stu[i].score != stu[i - 1].score) { 
 9     r = i + 1;
10     }
11 }

8、main() 最终形态

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

 

【算法学习记录-排序题】【PAT A1025】PAT Ranking

标签:clu   gen   follow   you   tar   创建   cstring   positive   mes   

原文地址:https://www.cnblogs.com/Bananice/p/12231523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!