【算法学习记录-排序题】【PAT A1062】Talent and Virtue
About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a "sage(圣人)"; being less excellent but with one's virtue outweighs talent can be called a "nobleman(君子)"; being good in neither is a "fool man(愚人)"; yet a fool man is better than a "small man(小人)" who prefers talent than virtue.
Now given the grades of talent and virtue of a group of people, you are supposed to rank them according to Sima Guang's theory.
Input Specification:
Each input file contains one test case. Each case first gives 3 positive integers in a line: N (≤105), the total number of people to be ranked; L (≥60), the lower bound of the qualified grades -- that is, only the ones whose grades of talent and virtue are both not below this line will be ranked; and H (<100), the higher line of qualification -- that is, those with both grades not below this line are considered as the "sages", and will be ranked in non-increasing order according to their total grades. Those with talent grades below H but virtue grades not are cosidered as the "noblemen", and are also ranked in non-increasing order according to their total grades, but they are listed after the "sages". Those with both grades below H, but with virtue not lower than talent are considered as the "fool men". They are ranked in the same way but after the "noblemen". The rest of people whose grades both pass the L line are ranked after the "fool men".
Then N lines follow, each gives the information of a person in the format:
ID_Number Virtue_Grade Talent_Grade
where ID_Number
is an 8-digit number, and both grades are integers in [0, 100]. All the numbers are separated by a space.
Output Specification:
The first line of output must give M (≤N), the total number of people that are actually ranked. Then M lines follow, each gives the information of a person in the same format as the input, according to the ranking rules. If there is a tie of the total grade, they must be ranked with respect to their virtue grades in non-increasing order. If there is still a tie, then output in increasing order of their ID's.
Sample Input:
14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60
Sample Output:
12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90
题意:
第一行给出三个整数:考生人数N,最低录取线L,优先录取线H
德分和才分均不低于L的才有录取资格
德才分均不低于H的为I类,最优先录取
德分不低于H但才分低于H的为II类,次优先录取
德分和才分均低于H但德分不低于才分的为III类,更次录取
其余考生为IV类,最次优先录取
思路:
1、排序的逻辑
①按类别从低到高排序
②类别相同,按总分从高到低排序
③总分相同,按德分从高到低排序
④德分相同,按考号从低到高排序
2、所需要的信息及信息的存储
①对于单个学生student,需要对应的准考证号id,德分de,才分cai,总分sum,类别flag——结构体Student,并创建一个足够大的结构体数组
②此外还需要总考生数n,最低分数线l,优先录取分数线h,录取考生数num——int型变量
3、排序逻辑的实现
bool cmp(Student a, Student b) {
if (a.flag != b.flag) {
return a.flag < b.flag;
} else if (a.sum != b.sum) {
return a.sum > b.sum;
} else if (a.de != b.de) {
return a.de > b.de;
} else {
return strcmp(a.id, b.id) < ;
}
}
4、main() 雏形
int main() {
int n, l, h, num = ;
scanf("%d%d%d", &n, &l, &h);
for (int i = ; i < n; i++) {
scanf("%s%d%d", stu[i].id, &stu[i].de, &stu[i].cai);
stu[i].sum = stu[i].de + stu[i].cai;
if (stu[i].de < l || stu[i].cai < l) {
stu[i].flag = ;
} else {
num++;
if (stu[i].de >= h && stu[i].cai >= h) {
stu[i].flag = ;
} else if (stu[i].de >= h && stu[i].cai < h) {
stu[i].flag = ;
} else if (stu[i] < h && stu[i].cai < h && stu[i].de >= stu[i].cai) {
stu[i].flag = ;
} else {
stu[i].flag = ;
}
}
}
sort(stu, stu + n, cmp);
}
5、优化
①逻辑优化
德分 < L | L <= 德分 < H | 德分 >= H | |
才分 < L | 5 | 5 | 5 |
L <= 才分 < H | 5 | 3 | 2 |
才分 >= H | 5 | 4 | 1 |
所以,if-else逻辑应简化为
if (stu[i].de < l || stu[i].cai < l) {
stu[i].flag = ;
} else if (stu[i].de >= h && stu[i].cai >= h) {
stu[i].flag = ;
} else if (stu[i].de >= h && stu[i].cai < h) {
stu[i].flag = ;
} else if (stu[i].de >= stu[i].cai) {
stu[i].flag = ;
} else {
stu[i].flag = ;
}
②num的替换
若在①中后四个else if中,每个都写上num++,会显得过于繁杂
因此将num初值设为参与考试的人数n,每次出现不合格的人时num--
int num = n; if (stu[i].de < l || stu[i].cai < l) {
stu[i].flag = ;
num--;
}
6、题解
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Student {
char id[];
int de;
int cai;
int sum;
int flag;
}stu[];
bool cmp(Student a, Student b) {
if (a.flag != b.flag) {
return a.flag < b.flag;
} else if (a.sum != b.sum) {
return a.sum > b.sum;
} else if (a.de != b.de) {
return a.de > b.de;
} else {
return strcmp(a.id, b.id) < ;
}
}
int main() {
int n, l, h;
scanf("%d%d%d", &n, &l, &h);
int num = n;
for (int i = ; i < n; i++) {
scanf("%s%d%d", stu[i].id, &stu[i].de, &stu[i].cai);
stu[i].sum = stu[i].de + stu[i].cai;
if (stu[i].de < l || stu[i].cai < l) {
stu[i].flag = ;
num--;
} else if (stu[i].de >= h && stu[i].cai >= h) {
stu[i].flag = ;
} else if (stu[i].de >= h && stu[i].cai < h) {
stu[i].flag = ;
} else if (stu[i].de >= stu[i].cai) {
stu[i].flag = ;
} else {
stu[i].flag = ;
}
}
sort(stu, stu + n, cmp);
printf("%d\n", num);
for (int i = ; i < num; i++) {
printf("%s %d %d\n", stu[i].id, stu[i].de, stu[i].cai);
}
}
*7、未解决的问题
题目中准考证号给出8位
但实际使用char[8]存储再输出时会出错???
【算法学习记录-排序题】【PAT A1062】Talent and Virtue的更多相关文章
- 【算法学习记录-排序题】【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 A1025】PAT Ranking
Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhe ...
- 算法学习记录-排序——插入排序(Insertion Sort)
插入排序: 在<算法导论>中是这样描述的 这是一个对少量元素进行排序的有效算法.插入排序的工作机理与打牌时候,整理手中的牌做法差不多. 在开始摸牌时,我们的左手是空的,牌面朝下放在桌子上. ...
- 算法学习记录-排序——冒泡排序(Bubble Sort)
冒泡排序应该是最常用的排序方法,我接触的第一个排序算法就是冒泡,老师也经常那这个做例子. 冒泡排序是一种交换排序, 基本思想: 通过两两比较相邻的记录,若反序则交换,知道没有反序的记录为止. 例子: ...
- 算法学习记录-排序——选择排序(Simple Selection Sort)
之前在冒泡排序的附录中提到可以在每次循环时候,不用交换操作,而只需要记录最小值下标,每次循环后交换哨兵与最小值下标的书, 这样可以减少交换操作的时间. 这种方法针对冒泡排序中需要频繁交换数组数字而改进 ...
- PAT甲级——A1062 Talent and Virtue
About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about ...
- PAT-B 1015. 德才论(同PAT 1062. Talent and Virtue)
1. 在排序的过程中,注意边界的处理(小于.小于等于) 2. 对于B-level,这题是比較麻烦一些了. 源代码: #include <cstdio> #include <vecto ...
- PAT 1062 Talent and Virtue[难]
1062 Talent and Virtue (25 分) About 900 years ago, a Chinese philosopher Sima Guang wrote a history ...
随机推荐
- MY_0003:设置界面显示单位
1,设置单位
- Jenkins-Publish Over SSH插件
插件安装 打开Jenkins的“系统管理>管理插件”,选择“可选插件”,在输入框中输入“Publish over SSH”进行搜索,如果搜索不到可以在“已安装”里确认是否已经安装过.在搜索结果中 ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
- Python三元表达式、列表推导式、生成器表达式
1. 三元表达式 name=input('姓名>>: ') res='SB' if name == 'aaaa' else 'NB' print(res) 2. 列表推导式 #1.示例 e ...
- Chrome Extension 记录
传递选定元素到内容脚本 内容脚本不能直接访问当前选中的元素.但是,任何使用 inspectedWindow.eval 来执行的代码都可以在 DevTools 控制台和命令行的 API 中使用.例如,在 ...
- 为什么要使用Redis? —— Redis实战经验
(序言,从一张思维导图开始,慢慢介绍我自己关于Redis的实战经验) 现在很多互联网应用的服务端都使用到了Redis,到底大家为什么要用Redis呢?Redis有很多特性,比如高性能.高可用.数据类型 ...
- 在linux中安装nginx
linux系统安装在vmware中,首先在主机中利用shell工具与虚拟机连接 1.在linux中查看虚拟机的ip地址 在终端输入 ifconfig 红框里面就是ip地址 2.在主机中打开shell工 ...
- 4P遇上了5P
(1)4P工作要素:任何一位从业者,都应该好好想想自己工作的初衷是什么?你将自己所从事的工作又是定位在什么位置?而这份工作的视角又有多宽.多广?最后是你会在某个周期内完成工作或者是实现突破. (2)5 ...
- pandas时间序列学习笔记
目录 创建一个时间序列 pd.date_range() info() asfred() shifted(),滞后函数 diff()求差分 加减乘除 DataFrame.reindex() 通过data ...
- day03_2spring3
SSH整合(续) 一.spring整合hibernate:有hibernate.cfg.xml 前提:导入jar包,在前面已经介绍了jar包的整合,我们只需要将整合的所有jar包导进去即可. 1.创建 ...