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 (≤10​5​​), 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的更多相关文章

  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 A1025】PAT Ranking

    Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhe ...

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

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

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

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

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

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

  7. PAT甲级——A1062 Talent and Virtue

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

  8. PAT-B 1015. 德才论(同PAT 1062. Talent and Virtue)

    1. 在排序的过程中,注意边界的处理(小于.小于等于) 2. 对于B-level,这题是比較麻烦一些了. 源代码: #include <cstdio> #include <vecto ...

  9. PAT 1062 Talent and Virtue[难]

    1062 Talent and Virtue (25 分) About 900 years ago, a Chinese philosopher Sima Guang wrote a history ...

随机推荐

  1. 请求筛选模块被配置为拒绝包含双重转义序列的请求(.net core程序的‘web.config’调整)

    之前项目有一个静态文件特殊字符转义的报错(+变为 %2B),老是显示404  请求筛选模块被配置为拒绝包含双重转义序列的请求  .网上的大多数解决方案都是一下: https://www.cnblogs ...

  2. 谷歌更新后,chromedriver如何更换新版本

    前天,更新了78版本的谷歌后,chromedriver便不能用了,于是在ChromeDriver仓库下载了相对应版本的chromedriver. 并且放入谷歌文件下C:\Program Files ( ...

  3. php外挂python脚本抓取ajax数据

    之前我写过一遍php外挂python脚本处理视频的文章.今天和大家分享下php外挂python实现输入关键字搜索的脚本 首先我们先来分析一波网站: http://www.dzdpw.com/s.php ...

  4. IDEA如何快速搭建Java开发环境

    作为IntelliJ IDEA mac新手,IDEA如何快速搭建Java开发环境呢?今天小编就给大家带来了IntelliJ IDEA mac使用教程,想知道IDEA如何快速搭建Java开发环境? 全局 ...

  5. Codeforces Round #613 (Div. 2) (A-E)

    A略 直接求和最大的子序列即可(注意不能全部选中整个子序列) or #include<bits/stdc++.h> using namespace std; void solve(){ i ...

  6. PAT (Basic Level) Practice (中文)1030 完美数列 (25 分) (有点意思)

    给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列. 输入格 ...

  7. 论文阅读笔记(十三)【arxiv2018】:Revisiting Temporal Modeling for Video-based Person ReID

    Introduction (1)Motivation: 当前的一些video-based reid方法在特征提取.损失函数方面不统一,无法客观比较效果.本文作者将特征提取和损失函数固定,对当前较新的4 ...

  8. 五、JVM之堆内存

    堆结构分代图 堆结构分代的意义 Java虚拟机根据对象存活的周期不同,把堆内存划分为几块,一般分为新生代.老年代和永久代(对HotSpot虚拟机而言),这就是JVM的内存分代策略. 堆内存是虚拟机管理 ...

  9. FatMouse and Cheese HDU - 1078 dp

    #include<cstdio> #include<iostream> #include<cstring> using namespace std; int n,k ...

  10. win下删除EFI分区

    管理员身份,在cmd终端下,用"diskpart"命令. diskpart ##命令进入Microsoft DiskPart 模式 list disk ##展示磁盘分区列表 sel ...