1062 Talent and Virtue

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

题意

给出 n 个考生的准考证号、德分、才分以及及格线 L 、优秀线 H ,然后对这 n 个考生进行分类:

①如果德分和才分中有一个低于 L ,则为不及格生,即为第5类,且设下面4类均及格
②如果德分和才分均不低于 H ,则为第1类。
③如果德分不低于 H ,才分低于 H ,则为第2类。
④如果德分和才分均低于 H 但德分不低于才分,则为第3类。
⑤剩余为第4类。对这 n 个考生按下面的规则排序:

排序的方法:

①先按类别从小到大排序:
②类别相同的,按总分从大到小排序
③总分相同的,按德分从大到小排序
④德分相同的,按准考证号从小到大排序
最后输出所有及格生的信息,顺序为排完序后的顺序

上代码

#include <cstdio>
#include <algorithm>
using namespace std;
const int mMax = 100010;
typedef struct rank{
	int id;
	int vir_gd;
	int tal_gd;
}RK;
RK ipt[mMax];
RK sage[mMax],nole[mMax],fool[mMax],small[mMax]; 
int n,l,h;
bool cmp(RK a,RK b){
	if(a.tal_gd+a.vir_gd==b.tal_gd+b.vir_gd){
		if(a.vir_gd==b.vir_gd) return a.id<b.id;
		else return a.vir_gd>b.vir_gd;
	}
	else return a.tal_gd+a.vir_gd>b.tal_gd+b.vir_gd;
}

int main(){
	scanf("%d%d%d",&n,&l,&h);
	int nn = n;
	int num = 0,sag_len=0,fol_len=0,nol_len=0,sml_len=0;	
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&ipt[i].id,&ipt[i].vir_gd,&ipt[i].tal_gd);
		if(ipt[i].tal_gd<l||ipt[i].vir_gd<l) num++;
		else if(ipt[i].tal_gd>=h&&ipt[i].vir_gd>=h) sage[sag_len++] = ipt[i];
		else if(ipt[i].vir_gd>=h&&ipt[i].tal_gd<h) nole[nol_len++] = ipt[i];
		else if(ipt[i].tal_gd<h&&ipt[i].vir_gd<h&&ipt[i].tal_gd<=ipt[i].vir_gd) fool[fol_len++] = ipt[i];
		else small[sml_len++] = ipt[i]; 
	}
	printf("%d\n",nn-num);
	if(sag_len){
		sort(sage,sage+sag_len,cmp);
		for(int i=0;i<sag_len;i++){
			printf("%d %d %d\n",sage[i].id,sage[i].vir_gd,sage[i].tal_gd);
		}	
	}
	
	if(nol_len){
		sort(nole,nole+nol_len,cmp);
		for(int i=0;i<nol_len;i++){
			printf("%d %d %d\n",nole[i].id,nole[i].vir_gd,nole[i].tal_gd);
		}
	}
	
	if(fol_len){
		sort(fool,fool+fol_len,cmp);
		for(int i=0;i<fol_len;i++){
			printf("%d %d %d\n",fool[i].id,fool[i].vir_gd,fool[i].tal_gd);
		}
	}
	
	if(sml_len){
		sort(small,small+sml_len,cmp);
		for(int i=0;i<sml_len;i++){
			printf("%d %d %d\n",small[i].id,small[i].vir_gd,small[i].tal_gd);
		}
	}
	return 0;
}

学习代码

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int mMax = 100010;
typedef struct rank{
	int id;
	int v;
	int t;
}RK;
RK ipt;
vector <RK> c[4]; 
int n,l,h;
bool cmp(RK a,RK b){
	if(a.t+a.v==b.t+b.v){
		if(a.v==b.v) return a.id<b.id;
		else return a.v>b.v;
	}
	else return a.t+a.v>b.t+b.v;
}

int main(){
	scanf("%d%d%d",&n,&l,&h);
	int nn = n;
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&ipt.id,&ipt.v,&ipt.t);
		if(ipt.t<l||ipt.v<l) nn--;
		else if(ipt.t>=h&&ipt.v>=h) c[0].push_back(ipt);
		else if(ipt.v>=h&&ipt.t<h) c[1].push_back(ipt);
		else if(ipt.t<h&&ipt.v<h&&ipt.t<=ipt.v) c[2].push_back(ipt);
		else c[3].push_back(ipt);
	}
	printf("%d\n",nn);
	for(int i=0;i<4;i++){
		sort(c[i].begin(),c[i].end(),cmp);
		for(int j=0;j<c[i].size();j++){
			printf("%d %d %d\n",c[i][j].id,c[i][j].v,c[i][j].t);
		}
	}
	return 0;
}

心得

  1. 命名不能太复杂,否则给自己遭罪,可读性也不会很好。
  2. 对于可以在输入的时候就进行可以进行操作的结构体,不需要构建结构体数组
  3. 对于数组的大小需要自己计算的时候,可以采用动态数组vector进行存储。

勉励
打卡第十五天,加油ヾ(◍°∇°◍)ノ゙

申明:学习的代码是借鉴了柳神小姐姐的PAT讲解。
如果觉得我的文章对你有所帮助与启发,点赞给我个鼓励吧(づ ̄3 ̄)づ╭❤~
如果文章有错误,还望不吝指教!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值