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、排序逻辑的实现

 bool cmp(Student a, Student b) {
if (a.score != b.score) {
return a.score > b.score;
} else {
return strcmp(a.id, b.id) < ;
}
}

4、main()  分步分析

主要分为两个部分:

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

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

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

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

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

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

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

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

 sort(stu, stu + k, cmp);

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

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

(2)给所有的考生排名

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

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

5、踩到了第一个坑

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

解决:

引入新变量 总考生数num

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

6、main()  雏形

 scanf("%d", &n); //考场数
for (int i = ; i <= n; i++) { //考场号,要从1开始
scanf("%d", &k); //当前考场人数 /*录入数据*/
for (int j = ; j < k; j++) {
scanf("%s %d", stu[num].id, &stu[num].score);
stu[num].location_number = i;
num++; //通过这个计算总考生数
} /*对刚录入的这个考场考生进行排序并赋名次*/
sort(stu + num - k, stu + num, cmp);
stu[num - k].local_rank = ;
for (int j = num - k + ; j < num; j++) {
if (stu[j].score == stu[j - ].score) {
stu[j].local_rank = stu[j - ].local_rank;
} else {
stu[j].local_rank = j + - (num - k);//j为此考生之前的人数,- (num - k)为去掉前面其他考场的人数,+ 1后即得到此考生在本考场的排名
}
} } /*对所有考生进行排序并赋名次*/
sort(stu, stu + num, cmp);
stu[].sum_rank = ;
for (int i = ; i < num; i++) {
if (stu[i].score == stu[i - ].score) {
stu[i].sum_rank = stu[i - ].sum_rank;
} else {
stu[i].sum_rank = i + ;
}
}

7、优化

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

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

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

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

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

8、main() 最终形态

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Student {
char id[];
int score;
int location_number;
int local_rank;
int sum_rank; //test
}stu[];
bool cmp(Student a, Student b) {
if (a.score != b.score) {
return a.score > b.score;
} else {
return strcmp(a.id, b.id) < ;
}
}
int main() {
int n, k, num = ;
scanf("%d", &n);
for (int i = ; i <= n; i++) {
scanf("%d", &k);
for (int j = ; j < k; j++) {
scanf("%s %d", stu[num].id, &stu[num].score);
stu[num].location_number = i;
num++;
}
sort(stu + num - k, stu + num, cmp);
stu[num - k].local_rank = ;
for (int j = num - k + ; j < num; j++) {
if (stu[j].score == stu[j - ].score) {
stu[j].local_rank = stu[j - ].local_rank;
} else {
stu[j].local_rank = j + - (num - k);
}
}
}
printf("%d\n", num);
sort(stu, stu + num, cmp);
int r = ;
for (int i = ; i < num; i++) {
if (i > && stu[i].score != stu[i - ].score) {
r = i + ;
}
printf("%s ", stu[i].id);
printf("%d %d %d\n", r, stu[i].location_number, stu[i].local_rank);
}
return ;
}

【算法学习记录-排序题】【PAT A1025】PAT Ranking的更多相关文章

  1. 【算法学习记录-排序题】【PAT A1012】The Best Rank

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  2. 【算法学习记录-排序题】【PAT A1016】Phone Bills

    A long-distance telephone company charges its customers by the following rules: Making a long-distan ...

  3. 【算法学习记录-排序题】【PAT A1062】Talent and Virtue

    About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about ...

  4. 算法学习记录-排序——插入排序(Insertion Sort)

    插入排序: 在<算法导论>中是这样描述的 这是一个对少量元素进行排序的有效算法.插入排序的工作机理与打牌时候,整理手中的牌做法差不多. 在开始摸牌时,我们的左手是空的,牌面朝下放在桌子上. ...

  5. 算法学习记录-排序——冒泡排序(Bubble Sort)

    冒泡排序应该是最常用的排序方法,我接触的第一个排序算法就是冒泡,老师也经常那这个做例子. 冒泡排序是一种交换排序, 基本思想: 通过两两比较相邻的记录,若反序则交换,知道没有反序的记录为止. 例子: ...

  6. 算法学习记录-排序——选择排序(Simple Selection Sort)

    之前在冒泡排序的附录中提到可以在每次循环时候,不用交换操作,而只需要记录最小值下标,每次循环后交换哨兵与最小值下标的书, 这样可以减少交换操作的时间. 这种方法针对冒泡排序中需要频繁交换数组数字而改进 ...

  7. 算法学习记录-图——应用之拓扑排序(Topological Sort)

    这一篇写有向无环图及其它的应用: 清楚概念: 有向无环图(DAG):一个无环的有向图.通俗的讲就是从一个点沿着有向边出发,无论怎么遍历都不会回到出发点上. 有向无环图是描述一项工程或者系统的进行过程的 ...

  8. 算法学习 拓扑排序(TopSort)

    拓扑排序 一.基本概念 在一个有向无环图(Directed Acyclic Graph, DAG)中,规定< u,v > 表示一条由u指向v的的有向边.要求对所有的节点排序,使得每一条有向 ...

  9. Manacher回文串算法学习记录

    FROM:  http://hi.baidu.com/chenwenwen0210/item/482c84396476f0e02f8ec230 #include<stdio.h> #inc ...

随机推荐

  1. Java基础之五、Java编程思想(1-7)

    一.对象导论 1:多态的可互换对象 面向对象程序设计语言使用了后期绑定的概念. 当向对象发送消息时,被调用的代码直到运行时才能确定.也叫动态绑定. 2:单根继承结构 所有的类最终都继承自单一的基类,这 ...

  2. Linux学习Day1:开班第一天

    其实这篇博客应该昨天就要写完的,算是补作业吧. 昨天(2020年2月14日)是参加Linux线上培训的第一天,当天培训结束后,老师要求学员每天写一篇博客来记录自己学到的知识,于是就有了这篇博客的诞生. ...

  3. day 17 初始递归

    递归函数 了解什么是递归 : 在函数中调用自身函数 最大递归深度默认是997/998 —— 是python从内存角度出发做的限制 能看懂递归 能知道递归的应用场景 初识递归 —— 算法 —— 二分查找 ...

  4. opencv —— Sobel 一阶导数算子、Scharr 滤波器 一阶导数用于边缘检测

    sobel 算子的基本概念 sobel 算子是一个主要用于边缘检测的离散微分算子,它结合了高斯平滑和微分求导,用于计算图像灰度函数的近似梯度. 其基础来自于一个事实,即在边缘部分,像素值出现“跳跃”或 ...

  5. C# WPF 时钟动画(2/2)

    模拟实现时钟效果,学习WPF动画好例子,本文承接上文 C# WPF 时钟动画(1/2). 微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. C# ...

  6. MS SQL为字段添加说明

    以ms sql server 14  v17为例. 如下表dbo.Q中有一个字段'' 首先在数据库的系统存储过程列表中: 找到sys.sp_addextendedproperty,使用这个为字段添加一 ...

  7. 剑指offer-面试题49-丑数-空间换时间

    /* 题目: 求从1开始的第n个丑数. */ /* 思路: 按顺序列出各个丑数. */ #include<iostream> #include<cstring> #includ ...

  8. 双向队列 SDUT 1466

    题目描述      想想双向链表……双向队列的定义差不多,也就是说一个队列的队尾同时也是队首:两头都可以做出队,入队的操作.现在给你一系列的操作,请输出最后队列的状态:命令格式:LIN X  X表示一 ...

  9. JS 自动关闭页面

    <script language=javascript> this.window.opener = null; window.close(); </script>

  10. nvm,nrm和yarn

    nvm Node Version Management nvm list 查看所有已安装的 node 版本 nvm install 版本号 安装指定版本的 node nvm use 版本号 切换到指定 ...