PAT乙级——德才论

题目描述

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

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

 

输入描述:

输入第1行给出3个正整数,分别为:N(<=105),即考生总数;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

代码一(由于判断语句过多,提交显示超时)

#include <iostream>
#include<algorithm>
using namespace std;

struct student {
	int id;
	int d_score;
	int c_score;
	int score;
}stu1[100010],stu2[100010],stu3[100010],stu4[100010];

int cmp(student a, student b)
{
	if (a.score != b.score) {
		return a.score > b.score;
	}
	else if (a.d_score != b.d_score) {
		return a.d_score > b.d_score;
	}
	else return a.id<b.id;

	
}
int main()
{
	int N, L, H, D_score, C_score,num1=0,num2=0,num3=0,num4=0,num;
	int ID;
	scanf("%d %d %d", &N, &L, &H);
	for (int i = 0; i < N; i++)
	{
		scanf("%d %d %d", &ID, &D_score, &C_score);
		if (D_score >= H && C_score >= H)
		{
			stu1[num1].c_score = C_score;
			stu1[num1].d_score = D_score;
			stu1[num1].id = ID;
			stu1[num1].score = C_score+D_score;
			num1++;
		}
		else if(D_score>=H&&C_score<H&&C_score>=L)
		{
			stu2[num2].c_score = C_score;
			stu2[num2].id = ID;
			stu2[num2].d_score = D_score;
			stu2[num2].score = C_score + D_score;
			num2++;
		}
		else if (D_score < H && C_score < H&& D_score >= C_score&&D_score>=L&&C_score>=L)
		{
			stu3[num3].c_score = C_score;
			stu3[num3].id = ID;
			stu3[num3].d_score = D_score;
			stu3[num3].score = C_score + D_score;
			num3++;
		}
		else if (D_score >= L && D_score < H&&C_score >= L &&D_score < C_score)
		{
			stu4[num4].c_score = C_score;
			stu4[num4].d_score = D_score;
			stu4[num4].id = ID;
			stu4[num4].score = C_score + D_score;
			num4++;
		}
		sort(stu1, stu1 + num1,cmp);
		sort(stu2, stu2 + num2,cmp);
		sort(stu3, stu3 + num3,cmp);
		sort(stu4, stu4 + num4,cmp);
		num = num1 + num2 + num3 + num4;
	}
	printf("%d\n", num);
	for (int i = 0; i < num1; i++)
	{
		printf("%d %d %d\n", stu1[i].id, stu1[i].d_score, stu1[i].c_score);
	}
	for (int i = 0; i < num2; i++)
	{
		printf("%d %d %d\n", stu2[i].id, stu2[i].d_score, stu2[i].c_score);
	}
	for (int i = 0; i < num3; i++)
	{
		printf("%d %d %d\n", stu3[i].id, stu3[i].d_score, stu3[i].c_score);
	}
	for (int i = 0; i < num4; i++)
	{
		printf("%d %d %d\n", stu4[i].id, stu4[i].d_score, stu4[i].c_score);
	}
	return 0;

}

代码二(思路同上,但是判断语句简明些,顺利通过)

#include<algorithm>
using namespace std;
struct student
{
	int number;
	int d;
	int c;
	int sum;
}stu[100000],stu1[100000],stu2[100000],stu3[100000],stu4[100000];
int cmp(struct student a, struct student b) {
    if (a.sum!=b.sum)
        return a.sum>b.sum;
    else if (a.d!=b.d)
        return a.d>b.d;
    else
        return a.number<b.number;
}
int main()
{
	int N,L,H,i,M=0;
	int j=0,k=0,l=0,m=0;
	scanf("%d %d %d",&N,&L,&H);
	for(i=0;i<N;i++)
	{
		scanf("%d %d %d",&stu[i].number,&stu[i].d,&stu[i].c);
		stu[i].sum=stu[i].d+stu[i].c;
	}
	for(i=0;i<N;i++)
	{
		if(stu[i].d>=L&&stu[i].c>=L)
		{
			M++;
			if(stu[i].d>=H&&stu[i].c>=H)
			{
				stu1[j]=stu[i];
				j++;
			}
			else if(stu[i].d>=H&&stu[i].c<H)
			{
				stu2[k]=stu[i];
				k++;
			}
			else if(stu[i].d<H&&stu[i].c<H&&stu[i].d>=stu[i].c)
			{
				stu3[l]=stu[i];
				l++;
			}
		    else 
			{
				stu4[m]=stu[i];
				m++;
			}
		}
	}
	sort(stu1, stu1+j, cmp);
	sort(stu2, stu2+k, cmp);
	sort(stu3, stu3+l, cmp);
	sort(stu4, stu4+m, cmp);
	printf("%d\n",M);
	for(i=0;i<j;i++)
	{
		printf("%d %d %d\n",stu1[i].number,stu1[i].d,stu1[i].c);
	}
	for(i=0;i<k;i++)
	{
		printf("%d %d %d\n",stu2[i].number,stu2[i].d,stu2[i].c);
	}
	for(i=0;i<l;i++)
	{
		printf("%d %d %d\n",stu3[i].number,stu3[i].d,stu3[i].c);
	}
	for(i=0;i<m;i++)
	{
		printf("%d %d %d\n",stu4[i].number,stu4[i].d,stu4[i].c);
	}
	return 0;
}

代码三(将成绩的五个类别也定义为整型数,加入到比较函数中用于后续排序,代码比较简洁)

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

struct Student {
	char id[10];
	int de, cai, sum;
	int flag;
}stu[100010];

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) < 0;

}

int main()
{
	int n, L, H;
	scanf("%d%d%d", &n, &L, &H);
	int m = n;
	for (int i = 0; i < n; i++)
	{
		scanf("%s%d%d", stu[i].id, &stu[i].de, &stu[i].cai);
		stu[i].sum = stu[i].cai + stu[i].de;
		if (stu[i].de < L || stu[i].cai < L) {
			stu[i].flag = 5;
			m--;
		}
		else if (stu[i].de >= H && stu[i].cai < H) stu[i].flag = 2;
		else if (stu[i].de > stu[i].cai) stu[i].flag = 3;
		else stu[i].flag = 4;
	}
	sort(stu, stu + n, cmp);
	printf("%d\n", m);
	for (int i = 0; i < m; i++)
	{
		printf("%s %d %d\n", stu[i].id, stu[i].de, stu[i].cai);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值