【算法学习记录-排序题】【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 ... 
随机推荐
- 用记事本编辑HTML文件后保存代码全堆在一起了,记事本打开html文件格式乱了
			经常会遇到这么一个现象,记事本打开编辑html代码,保存后格式就乱了,代码全部堆在一行了.遇到这种情况有时候也很无语 因为平常工作中也经常遇到这样的情况,后来通过研究,大概找到问题的所在. 我是这么一 ... 
- unity目前学的一些操作
			目前是根据b站的一位迈扣老师的30集基础教学学习的,用的是sunny land这个资源包进行的教学,这位老师讲得很清晰,吐词清晰,思路也清晰,推荐哦.其实我比较喜欢这样的老师,思路 吐词清晰.就像以前 ... 
- [译]C# 7系列,Part 10: Span<T> and universal memory management Span<T>和统一内存管理
			原文:https://blogs.msdn.microsoft.com/mazhou/2018/03/25/c-7-series-part-10-spant-and-universal-memory- ... 
- java流程控制结构
			一.流程控制分三类 1. 顺序结构 - 程序是从上往下,从左往右执行 2. 选择结构(判断结构) - if语句 A. if(关系表达式){语句体} - 执行流程:成立就执行语句体,不成立就不执行 B. ... 
- P1282 多米诺骨牌【dp】
			P1282 多米诺骨牌 提交 20.02k 通过 6.30k 时间限制 1.00s 内存限制 125.00MB 题目提供者洛谷 难度提高+/省选- 历史分数100 提交记录 查看题解 标签 查看算 ... 
- LeetCode 572. 另一个树的子树
			题目链接:https://leetcode-cn.com/problems/subtree-of-another-tree/ 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和 ... 
- .netCore MVC View 如何不使用模板
			默认情况下, 新建的View都是默认加载模板 _Laytou.cshtml 文件的,这是因为在 _ViewStart.cshtml 中定义了. 如果不需要加载模板文件,有两种方法 1. 在单独的Vie ... 
- waiting list
			Problem: how to cluster non-stationary multivariate time series. What are stationary time series How ... 
- python3练习100题——050
			题目:输出一个随机数. 程序分析:使用 random 模块. import random print( random.randint(1,10) ) # 产生 1 到 10 的一个整数型随机数 pri ... 
- 小白月赛22 F: 累乘数字
			F:累乘数字 考察点: 思维,高精度 坑点 : 模拟就 OK 析题得侃: 如果你思维比较灵敏:直接输出这个数+ d 个 "00"就行了 当然,我还没有那么灵敏,只能用大数来搞了 关 ... 
