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. Saltshaker 开源的基于Saltstack的Web 配管工具,欢迎使用

    Saltshaker是基于saltstack开发的以Web方式进行配置管理的运维工具,简化了saltstack的日常使用,丰富了saltstack的功能,支持多Master的管理. 已经在GitHub ...

  2. beautifulsoup4进阶学习笔记

    requests库是可以找到想要的东西,基本上几行代码就搞定,但是进一步把有用的内容提取出来变成自己想要的格式来方便后续进行数据分析 正则表达式提取的话,需要一些时间成本,这个可以每天积累一点. 这里 ...

  3. ASP.NET Identity系列教程-3【运用ASP.NET Identity】

    https://www.cnblogs.com/r01cn/p/5180892.html 14 运用ASP.NET Identity In this chapter, I show you how t ...

  4. appium 爬取微信的相册内容(不知什么时候能写完)

    # crowl wechat through appium from appium import webdriver from selenium.webdriver.support.ui import ...

  5. 3.Docker Compose 部署 GitLab

    什么是 GitLab GitLab 是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托管的 Git 项目仓库,可通过 Web 界面进行访问公开的或者私人项目.它拥有与 Githu ...

  6. unity踩坑2020-01-21

    这几天一直在测试一个类似于传奇的2d界面游戏,目前做的测试为: 人物动作响应,主要是8方向的判断和资源文件精灵的刷新. 学到的知识点: 1,Enum.GetHashCode() 可以得到这个枚举的索引 ...

  7. pip问题:Traceback (most recent call last): File "/usr/bin/pip", line 9, in

    源作者blog https://blog.csdn.net/vmxhc1314/article/details/81869676 编辑提示的文件,进行更改即可. 解决方法: 将 /usr/bin/pi ...

  8. 另外一种获取redis cluster主从关系和slot分布的方法

    条条大路通罗马,通过最近学习redis cluster 观察其输出,发现了另外一种获取master-slave关系的方法. [redis@lxd-vm1 ~]$ cat get_master_slav ...

  9. java - synchronized与lock的区别

    synchronized与lock的区别 原始构成 synchronized是关键字属于JVM层面 monitorenter(底层是通过monitor对象来完成,其实wait/notify等对象也依赖 ...

  10. day03_1spring3

    事务管理的几种方式.spring整合Junit.spring整合web.ssh整合 一.事务管理的几种方式: 1.介绍前提我们需要导入:spring-tx-3.2.0.RELEASE.jar的包里面含 ...