题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805307551629312
分析:题目也需要好好理解;意思是,把所有学生分成几类,一类是德才都不低于优先,一类是德不低于优秀,才低于优秀,一类是德才都低于优秀,一类是德才低于及格成绩;
解题时,用struct处了保存基本三种信息,还要判断从而将学生类别记录;sort的排序函数是存在多个关键字;最后还有一个就是如果数据大小在超时的边缘(10^5~10^6),用scanf,printf输入输出;
代码:
#include<iostream>
using namespace std;
#include<cstring>
#include<string>
#include<cctype>
#include<cmath>
#include<vector>
#include<algorithm>
struct student{
long long int num;
int d,c;
int group; //记录类别
}stu;
vector<student> s;
//多关键字排序,返回true 和 false
bool cmp(student s1,student s2)
{
if(s1.group<s2.group) return true;
else if(s1.group>s2.group) return false;
else
{
if(s1.d+s1.c==s2.d+s2.c)
{
if(s1.d==s2.d) return s1.num<s2.num;
return s1.d>s2.d;
}
return s1.d+s1.c>s2.d+s2.c;
}
}
int main()
{
int n,l,h;
scanf("%d %d %d",&n,&l,&h);
for(int i=0;i<n;++i)
{
scanf("%lld %d %d",&stu.num,&stu.d,&stu.c);
if(stu.c<l||stu.d<l) continue; //判断类别
else if(stu.d>=h&&stu.c>=h) stu.group=1;
else if(stu.d>=h) stu.group=2;
else if(stu.d<h&&stu.c<h&&stu.d>=stu.c) stu.group=3;
else stu.group=4;
s.push_back(stu);
}
sort(s.begin(),s.end(),cmp);
n=s.size();
cout<<n<<endl;
for(int i=0;i<n;++i)
printf("%lld %d %d\n",s[i].num,s[i].d,s[i].c);
return 0;
}