1015 德才论 (25)(25 分)详解与双版本代码

本题链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805307551629312

1015 德才论 (25)(25 分)

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式:

输入第1行给出3个正整数,分别为:N(<=10^5^),即考生总数;L(>=60),为录取最低分数线,即德分和才分均不低于L的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线L的考生也按总分排序,但排在第三类考生之后。

随后N行,每行给出一位考生的信息,包括:准考证号、德分、才分,其中准考证号为8位整数,德才分为区间[0, 100]内的整数。数字间以空格分隔。

输出格式:

输出第1行首先给出达到最低分数线的考生人数M,随后M行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:

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

输出样例:

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

分析:

本题确实不简单,主要是要知道要用什么样的方法,不过在讲述方法前,先来梳理下题目的细节,假设德分为d,才分为c:

1.考生类别:

本题有4类考生,这些考生必须先满足

d>=L&&c>=L

然后每类考生要满足一定条件:

第1类考生:d>=H&&c>=H

第2类考生:注意题目所说的“德分到线”是指德分不小于线而不是等于线,所以条件为

d>=H&&c<H

第3类考生:d<H&&c<H&&d>=c

第4类考生:除上述3类考生外的考生

2.隐藏细节:本题需要大量的IO,故C++一般使用STL的sort和关闭输入输出同步的语句,还有endl耗时过长,会超时,一般用’\n’或“\n”替代

分析完细节,需要选择恰当的方法,一般用结构体存储考号,德分,才分。然后用能存储结构体的线性结构来存储,C++一般用vector,C语言一般用结构体数组,存储完后进行排序。C++使用STL的 sort函数,C语言则用qsort

代码:

C++版

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct Student{
	int stid,de,cai;	
};//定义结构体,成员为考号,德分,才分 
bool cmp(Student a,Student b)//设计适应题目的比较函数以调用排序算法 
{ 
	if(a.de+a.cai!=b.de+b.cai)
	return a.de+a.cai>b.de+b.cai;//降序 
	else
	{
		if(a.de!=b.de)
		return a.de>b.de;//降序 
		else
		return a.stid<b.stid;//升序 
	}
}
int main()
{
	std::ios::sync_with_stdio(false);//注意加上这句以应对大量IO 
	int N,L,H,M=0;
	Student t;//定义结构体变量 
	vector<Student>stu1,stu2,stu3,stu4;//使用vector线性存储结构体,以方便调用排序算法 
	cin>>N>>L>>H;
	for(int i=0;i<N;i++)
	{
		cin>>t.stid>>t.de>>t.cai;
		if(t.de>=L&&t.cai>=L)
		{
			M++;//统计达到最低分数线的考生人数
			if(t.de>=H&&t.cai>=H)
			stu1.push_back(t);
			else if(t.de>=H&&t.cai<H)//才分不到但德分到线,德分到线的意思是德分不小于此线 
			stu2.push_back(t);
			else if(t.de<H&&t.cai<H&&t.de>=t.cai)
			stu3.push_back(t);
			else 
			stu4.push_back(t);
		}
	}
	//调用排序算法 
	sort(stu1.begin(),stu1.end(),cmp);
	sort(stu2.begin(),stu2.end(),cmp);
	sort(stu3.begin(),stu3.end(),cmp);
	sort(stu4.begin(),stu4.end(),cmp);
	//输出,注意换行时使用endl耗时很长,会超时,故用'\n'或“\n” 
	cout<<M<<'\n';
	for(int i=0;i<stu1.size();i++)
	cout<<stu1[i].stid<<' '<<stu1[i].de<<' '<<stu1[i].cai<<'\n';
	for(int i=0;i<stu2.size();i++)
	cout<<stu2[i].stid<<' '<<stu2[i].de<<' '<<stu2[i].cai<<'\n';
	for(int i=0;i<stu3.size();i++)
	cout<<stu3[i].stid<<' '<<stu3[i].de<<' '<<stu3[i].cai<<'\n';
	for(int i=0;i<stu4.size();i++)
	cout<<stu4[i].stid<<' '<<stu4[i].de<<' '<<stu4[i].cai<<'\n';
	return 0;
}

C语言版

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100000
struct Student{
	int stid,de,cai;	
}stu1[MAXSIZE],stu2[MAXSIZE],stu3[MAXSIZE],stu4[MAXSIZE];
typedef struct Student ElemType;
int Compare(const void *A, const void *B)
{
	ElemType *a=(ElemType*)A;
	ElemType *b=(ElemType*)B;
	int d1=a->de,d2=b->de;
	int c1=a->cai,c2=b->cai;
	int id1=a->stid,id2=b->stid;
	int total1=d1+c1,total2=d2+c2;
	if(total1!=total2)
	return total2-total1;
	else
	{
		if(d1!=d2)
		return d2-d1;
		else
		return id1-id2;
	}
}
int main()
{
	int N,L,H;
	struct Student t;
	scanf("%d%d%d",&N,&L,&H);
	int M=0,i,size1=0,size2=0,size3=0,size4=0;
	while(N--)
	{
		scanf("%d%d%d",&t.stid,&t.de,&t.cai);
		if(t.de>=L&&t.cai>=L)
		{
			M++;
			if(t.de>=H&&t.cai>=H)
			stu1[size1++]=t;
			else if(t.de>=H&&t.cai<H)
			stu2[size2++]=t;
			else if(t.de<H&&t.cai<H&&t.de>=t.cai)
			stu3[size3++]=t;
			else
			stu4[size4++]=t;
		}
	}
	qsort(stu1,size1,sizeof(ElemType),Compare);
	qsort(stu2,size2,sizeof(ElemType),Compare);
	qsort(stu3,size3,sizeof(ElemType),Compare);
	qsort(stu4,size4,sizeof(ElemType),Compare);
	printf("%d\n",M);
	for(i=0;i<size1;i++)
	printf("%d %d %d\n",stu1[i].stid,stu1[i].de,stu1[i].cai);
	for(i=0;i<size2;i++)
	printf("%d %d %d\n",stu2[i].stid,stu2[i].de,stu2[i].cai);
	for(i=0;i<size3;i++)
	printf("%d %d %d\n",stu3[i].stid,stu3[i].de,stu3[i].cai);
	for(i=0;i<size4;i++)
	printf("%d %d %d\n",stu4[i].stid,stu4[i].de,stu4[i].cai);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37729102/article/details/81633082