题目叙述
中文翻译
这题目不是很难就是一个排序然后分类的问题,但是书上面的方法利用了sort函数的一些特性。sort后面可以比较函数。
代码如下:
#include <iostream>
#include <cstdio>
#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;
cin>>n>>L>>H;
int m=n;//m是及格人数
int i;
for(i=0;i<n;i++)
{
cin>>stu[i].id>>stu[i].de>>stu[i].cai;
stu[i].sum=stu[i].de+stu[i].cai;
cout<<stu[i].id<<" "<<stu[i].de<<" "<<stu[i].cai<<endl;
if(stu[i].cai<L || stu[i].de<L)
{
stu[i].flag=5;
m--;
}
else if(stu[i].de>=H && stu[i].cai>=H) stu[i].flag=1;
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);
cout<<m<<endl;
for(i=0;i<m;i++)
{
cout<<stu[i].id<<" "<<stu[i].de<<" "<<stu[i].cai<<endl;
}
return 0;
}