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. LAMP环境搭建与配置(3)

    PHP配置 查看PHP配置文件的位置 #  /usr/local/php/bin/php   -i  |grep  -i  "loaded configuration file" ...

  2. BZOJ 4247: 挂饰 动态规划

    按照挂件数量排序,然后做一个 DP 就好了. code: #include <bits/stdc++.h> #define ll long long #define N 2003 #def ...

  3. 最短路径算法总结(floyd,dijkstra,bellman-ford)

    继续复习数据结构和算法,总结一下求解最短路径的一些算法. 弗洛伊德(floyd)算法 弗洛伊德算法是最容易理解的最短路径算法,可以求图中任意两点间的最短距离,但时间复杂度高达\(O(n^3)\),主要 ...

  4. 剑指offer-面试题12-矩阵中的路径-回溯法

    /* 题目: 设计一个函数,判断一个矩阵中是否存在一条包含该字符串所有字符的路径. 路径可从字符串的任意一格开始,每一步可向上.下.左.右移动一格. 如果一条路径经过了矩阵中的某一格,那么该路径不能再 ...

  5. @Html.DropDownList 无法显示默认值问题

    1.查看下拉框的id名,是否和ViewBag变量名相同,如果相同的话,更改下拉框的id名字,使之和ViewBag变量名相同即可例如:@Html.DropDownList("List1&quo ...

  6. C++野指针的存在方式和误区

    1. char* x;这样的一定是野指针,指针声明时要直接初始化!或者置null也行! 2. int main() { char *x=new char; delete x; cout<< ...

  7. kanbanflow的使用

    也许在工作中大家都听说过番茄工作法,就是每次在一个番茄钟25分钟内保持高度专注,并且在时间结束的时候会提醒你,然后稍作休息5分钟:此外,在产品迭代开发过程中常常会接受到不同的task:那么,我们是否可 ...

  8. 记录 Docker 的学习过程 (网络篇之跨主机互通)

    下面从node3上操作node3# docker run -d -p 8500:8500 --name consul progrium/consul -server -bootstrap node3# ...

  9. Selenium3+python自动化012+日志logging基本用法、高级用法

    1.关键字: login 登录 log 日志 logging python日志模块 2.什么叫日志: 日志用来记录用户行为或者代码的执行过程 3.日志使用的地方: 1.排错的时候需要打印很多细节来帮助 ...

  10. Eclipse设置代码模板

    个人博客 地址:http://www.wenhaofan.com/article/20180904173808 根据下列路径打开配置窗口 Window->Preferences->Java ...