1015德才论 C++ sort()、cmp()函数实现 仅46行代码 AC

思路:

  1. 通过考生分数判断考生属于第几类(level()函数)
  2. 不同类别的考生比类别,类别越高排名越靠前
  3. 相同类别的考生比总分降序>德分降序>准考证号升序
#include<iostream>
#include<algorithm>
using namespace std;
struct student{
	int id;
	int de;
	int cai;
}stu[100001];
int n,l,h;
int level(student a){
	if(a.de>=h&&a.cai>=h) return 4;
	else if(a.de>=h&&a.cai<h&&a.cai>=l) return 3;
	else if(a.de>=a.cai&&a.cai<h&&a.cai>=l&&a.de<h&&a.de>=l) return 2;
	else if(a.de<l||a.cai<l) return 0;
	else return 1;
}
bool cmp(student a,student b){
	int a_all=a.de+a.cai,b_all=b.de+b.cai;
	if(level(a)!=level(b)) return level(a)>level(b); //不同类别考生比等级 
	else{ //相同类别考生比总分降序>德分降序>准考证号升序 
		if(a_all==b_all){
			if(a.de==b.de){
				return a.id<b.id;
			}else{
				return a.de>b.de;
			}
		}else{
			return a_all>b_all;
		}
	}
}
int main(){
	
	cin>>n>>l>>h;
	int uplow=0;
	for(int i=0;i<n;i++){
		cin>>stu[i].id>>stu[i].de>>stu[i].cai;
		if(stu[i].de>=l&&stu[i].cai>=l) uplow++;
	}
	sort(stu,stu+n,cmp);
	cout<<uplow<<endl;
	for(int i=0;i<n;i++){
		if(stu[i].de<l||stu[i].cai<l) continue;
		cout<<stu[i].id<<" "<<stu[i].de<<" "<<stu[i].cai;
		if(i!=n-1) cout<<endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nabobess

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值