PAT甲级1062

本文探讨了司马光的历史哲学思想中关于人才的德与才的理论,并将其应用于现代算法中,通过设定不同的分数线来区分圣人、君子、小人和愚人,实现对一组人的综合排名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

				先把题目奉上	德才论					
										1062 Talent and Virtue (25 分)

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

这题的意思就是给m个人,每个人有自己的ID以及他们的道德分数和才能分数。
对于这两个指数分别给出它们的及格指数low和优秀指数high
所以我写了一个结构体用来存储每个人的德和才,以及他们的德才总分和等级

struct person
{
	int num;
	int vir,gra,sum;
	int flag;
}p[100010];

德和才都<low的被淘汰
德和才都>=high的为圣人
德>=high且low<=才<=high的为君子
low<=德<=high且才>=high的为小人
low<=德和才<=high为愚人
处于同一等级且分数相同者按ID从小到大排列
对应的代码就是
(vir对应德,gra对应才)

		if(p[i].vir<low||p[i].gra<low){
			m--;
			p[i].flag=5;
		}
		else if(p[i].vir>=high&&p[i].gra>=high)
		p[i].flag=1;
		else if(p[i].vir>=high&&p[i].gra<high)
		p[i].flag=2;
		else if(p[i].vir>=p[i].gra)
		p[i].flag=3;
		else
		p[i].flag=4;

每个人的信息已经建立好,等级也已经划分好,接下来就是对他们进行排序
我使用的是c++的algorithm库函数里的sort()函数,快排+堆排+插入哦,效率很高NlogN
重点来了 cmp!之前接触cmp是对字符型数组进行排序的时候使用的,在这里对cmp进行一个更深层次的学习和巩固。因为sort()里可以是sort(,)也可以是sort(,cmp)。前者可以进行升序比较,而后者不一样了后者可以更改排序的准则,bool cmp()的返回值是1则不需要改变排序的顺序,是负的则要进行对调
因为我们需要先对等级进行排列,再对相同等级里的人进行对应规则的排列,分数大小排列以及ID降序

bool cmp(person x,person y)
{
	if(x.flag!=y.flag)return x.flag<y.flag;
	else if(x.sum!=y.sum)return x.sum>y.sum;
	else if(x.vir!=y.vir)return x.vir>y.vir;
	else return x.num<y.num;
}

完整的代码也呼之欲出了

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct person
{
	int num;
	int vir,gra,sum;
	int flag;
}p[100010];
bool cmp(person x,person y)
{
	if(x.flag!=y.flag)return x.flag<y.flag;
	else if(x.sum!=y.sum)return x.sum>y.sum;
	else if(x.vir!=y.vir)return x.vir>y.vir;
	else return x.num<y.num;
}
int main()
{
	int m,low,high,n;
	cin>>m>>low>>high;
	n=m;
	for(int i=0;i<n;i++)
	{
		cin>>p[i].num>>p[i].vir>>p[i].gra;
		p[i].sum=p[i].vir+p[i].gra;
		if(p[i].vir<low||p[i].gra<low){
			m--;
			p[i].flag=5;
		}
		else if(p[i].vir>=high&&p[i].gra>=high)
		p[i].flag=1;
		else if(p[i].vir>=high&&p[i].gra<high)
		p[i].flag=2;
		else if(p[i].vir>=p[i].gra)
		p[i].flag=3;
		else
		p[i].flag=4;
	}
	sort(p,p+n,cmp);
	cout<<m<<'\n';
	for(int i=0;i<m;i++){
		cout<<p[i].num<<" "<<p[i].vir<<" "<<p[i].gra<<'\n';
	}
	return 0;
} 

本人实力有限,各位大佬帮忙评论纠错,谢谢

### 关于 PAT 甲级 1024 题目 PAT (Programming Ability Test) 是一项编程能力测试,其中甲级考试面向有一定编程基础的学生。对于 PAT 甲级 1024 题目,虽然具体题目描述未直接给出,但从相似类型的题目分析来看,这类题目通常涉及较为复杂的算法设计。 #### 数据结构的选择与实现 针对此类问题,常用的数据结构包括但不限于二叉树节点定义: ```cpp struct Node { int val; Node* lchild, *rchild; }; ``` 此数据结构用于表示二叉树中的节点[^1]。通过这种方式构建的二叉树能够支持多种遍历操作,如前序、中序和后序遍历等。 #### 算法思路 当处理涉及到图论的问题时,深度优先搜索(DFS)是一种常见的解题策略。特别是当需要寻找最优路径或访问尽可能多的节点时,结合贪心算法可以在某些情况下提供有效的解决方案[^2]。 #### 输入输出格式说明 根据以往的经验,在解决 PAT 类型的问题时,输入部分往往遵循特定模式。例如,给定 N 行输入来描述每个节点的信息,每行按照如下格式:“Address Data Next”,这有助于理解如何解析输入并建立相应的数据模型[^4]。 #### 数学运算示例 有时也会遇到基本算术表达式的求值问题,比如分数之间的加减乘除运算。下面是一些简单的例子展示不同情况下的计算结果: - \( \frac{2}{3} + (-2) = -\frac{7}{3}\) -2) = -\frac{4}{3}\) - \( \frac{2}{3} ÷ (-2) = -\frac{1}{3}\) 这些运算是基于样例提供的信息得出的结果[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值