PAT B1015 德才论

原题链接:点击打开链接

重点是比较函数cmp的写法,写得不好会写很多复杂的代码。

还有就是要先排除下界,从而避免判断1、2、3、4类考生时判断条件太多而超时。

也要注意输出最好用printf,cout可能会超时

#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h>
using namespace std;
struct student {
	char Num[9];
	int D = 0;
	int C = 0;
	int sum = 0;
	int type;
}stu[100010];
bool cmp(student a, student b) {
	if (a.type != b.type) return a.type < b.type;
	else if(a.sum != b.sum) return a.sum > b.sum; 
	else if (a.D != b.D)  return a.D > b.D; 
	else return strcmp(a.Num, b.Num)<0;
}
int main() {
	int N, L, H;//N为考生总数,L最低录取分数线,H优先录取线,D德分,C才分
	int M = 0;//过线人数
	N = 0;
	cin >> N >> L >> H;
	for (int i = 0; i < N; i++)
	{
		cin >> stu[i].Num >> stu[i].D >> stu[i].C;
		stu[i].sum = stu[i].C + stu[i].D;
		if (stu[i].D < L || stu[i].C < L)
		{
			stu[i].type = 5;
		}
		else if (stu[i].D >= H && stu[i].C >= H)
		{
			stu[i].type = 1;
			M++;
		}
		else if(stu[i].D >= H && stu[i].C <= H)
		{
			stu[i].type = 2;
			M++;
		}
		else if (stu[i].D >= stu[i].C)
		{
			stu[i].type = 3;
			M++;
		}
		else
		{
			stu[i].type = 4;
			M++;
		}
	}
	sort(stu, stu + N, cmp);
	printf("%d\n", M);
	for (int i = 0; i < M; i++)
	{
		printf("%s %d %d\n", stu[i].Num, stu[i].D, stu[i].C);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值