分析:
1.分类:
- 德>=H,才>=H(第一类)
- 德>=H,才>=L(第二类)
- 德>=L,才>=L,德>=才(第三类)
- 德>=L,才>=L(第四类)
- 其他不满足条件者(第五类)
2.排序:第一类>第二类>第三类>第四类>第五类;其次,类内排序按题目给的“总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出”
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef struct student{
string id;//编号
int moral;//德
int ability;//才
int priority;//优先级
int sum;//总分
void setSum(){
sum = moral + ability;
}
void setPriority(int L, int H){//设置优先级
if(moral >= H && ability >= H){
priority = 1;
} else if(moral >= H && ability >= L){
priority = 2;
} else if(moral >= L && ability >= L && moral >= ability){//德才相等也归为第三类
priority = 3;
} else if(moral >= L && ability >= L){
priority = 4;
} else{
priority = 5;
}
}
}student;
bool cmp(student a, student b){
if(a.priority == b.priority){//优先级相同,按总分排序
if(a.sum == b.sum){//总分相同,按德分排序
if(a.moral == b.moral){//德分相同,按编号排序
return a.id < b.id;//编号小者在前
}
return a.moral > b.moral;//德分大者在前
}
return a.sum > b.sum;//总分大者在前
}
return a.priority < b.priority;//类别小者在前
}
int main()
{
std::ios::sync_with_stdio(false);//一定一定要加上这句话,否则会超时
int N, L, H;
cin>>N>>L>>H;
student stud[N+8];//如果[]内有变量,该语句只能在这,否则会报空指针错误
for(int i=0; i<N; i++){
cin>>stud[i].id>>stud[i].moral>>stud[i].ability;
stud[i].setSum();
stud[i].setPriority(L, H);
}
sort(stud, stud+N, cmp);//排序后,第五类人会排在最后
int total = N;
for(; stud[total-1].priority==5; total--);//逆序查找第五类中第一个的位置;也可以用二分查找
cout<<total<<endl;
for(int i=0; i<total; i++){
cout<<stud[i].id<<" "<<stud[i].moral<<" "<<stud[i].ability<<endl;
}
return 0;
}