【算法学习记录-排序题】【PAT A1025】PAT Ranking
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的更多相关文章
- 【算法学习记录-排序题】【PAT A1012】The Best Rank
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- 【算法学习记录-排序题】【PAT A1016】Phone Bills
A long-distance telephone company charges its customers by the following rules: Making a long-distan ...
- 【算法学习记录-排序题】【PAT A1062】Talent and Virtue
About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about ...
- 算法学习记录-排序——插入排序(Insertion Sort)
插入排序: 在<算法导论>中是这样描述的 这是一个对少量元素进行排序的有效算法.插入排序的工作机理与打牌时候,整理手中的牌做法差不多. 在开始摸牌时,我们的左手是空的,牌面朝下放在桌子上. ...
- 算法学习记录-排序——冒泡排序(Bubble Sort)
冒泡排序应该是最常用的排序方法,我接触的第一个排序算法就是冒泡,老师也经常那这个做例子. 冒泡排序是一种交换排序, 基本思想: 通过两两比较相邻的记录,若反序则交换,知道没有反序的记录为止. 例子: ...
- 算法学习记录-排序——选择排序(Simple Selection Sort)
之前在冒泡排序的附录中提到可以在每次循环时候,不用交换操作,而只需要记录最小值下标,每次循环后交换哨兵与最小值下标的书, 这样可以减少交换操作的时间. 这种方法针对冒泡排序中需要频繁交换数组数字而改进 ...
- 算法学习记录-图——应用之拓扑排序(Topological Sort)
这一篇写有向无环图及其它的应用: 清楚概念: 有向无环图(DAG):一个无环的有向图.通俗的讲就是从一个点沿着有向边出发,无论怎么遍历都不会回到出发点上. 有向无环图是描述一项工程或者系统的进行过程的 ...
- 算法学习 拓扑排序(TopSort)
拓扑排序 一.基本概念 在一个有向无环图(Directed Acyclic Graph, DAG)中,规定< u,v > 表示一条由u指向v的的有向边.要求对所有的节点排序,使得每一条有向 ...
- Manacher回文串算法学习记录
FROM: http://hi.baidu.com/chenwenwen0210/item/482c84396476f0e02f8ec230 #include<stdio.h> #inc ...
随机推荐
- java循环语句 总结笔记
1.for 循环语句 语法:for(initialization;condition;iteration) public class A { public static void main(Strin ...
- 【DTOJ】2703:两个数的余数和商
DTOJ 2703:两个数的余数和商 解题报告 2017.11.10 第一版 ——由翱翔的逗比w原创,引用<C++ Primer Plus(第6版)中文版> 题目信息: 题目描述 给你a ...
- ftp下载目录文件 不需要ftp脚本
ftp下载目录文件 不需要ftp脚本 wget ftp://192.168.1.37:21/checkpoints --ftp-user=ftpadmin --ftp-password=gaofeng ...
- Android一个简单的自定义对话框制作
布局文件 <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android= ...
- 【Spring】事务(transactional) - REQUIRES_NEW在JdbcTemplate、Mybatis中的不同表现
环境 数据库: oracle 11g JAR: org.springframework:spring-jdbc:4.3.8.RELEASE org.mybatis:mybatis:3.4.2 概念 R ...
- Mac上的屏幕截图不起作用该如何修复?
屏幕截图是Mac提供的内置功能,很少有它不起作用.但是由于某些意外的设置或硬件问题,Mac上的屏幕截图有时无法正常工作,这里提供的是Mac上的屏幕截图不起作用该如何修复? 1.在Mac上启用屏幕快照快 ...
- C++文件读写demo
一.常用文件打开方式 二.读写文件步骤(文本文件) 1.写文件步骤 1)#include <fstream> //包含头文件 2)ofstream ofs; //创建流对象 3)ofs.o ...
- Vue中富文本编辑器(vue-quill-editor)的使用
1. 安装 npm install vue-quill-editor --save 2. 导入并挂载 import VueQuillEditor from 'vue-quill-editor' // ...
- 题解【洛谷P2730】魔板 Magic Squares
题面 首先我们可以发现,在每一次 BFS 时按照 \(A→B→C\) 的顺序枚举遍历肯定是字典序最小的. 然后就是普通的 BFS 了. 我们考虑使用 \(\text{STL map}\) 来存储起点状 ...
- PHPMailer发送邮件遇坑小记
一:phpmailer发送邮件成功了 数据库发送状态也更改 但是用户就是没收到邮件. 出现原因:发送邮件太多 导致邮箱服务器被腾讯封了 发送的邮件统统进入了邮件服务器的草稿箱里. 解决方案: 重新修改 ...