PAT甲级 1062 Talent and Virtue 排序+模拟
作者:互联网
代码如下:
//排序
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
struct people{
int id;
int v_grade;//美德成绩
int t_grade;//智力成绩
int total;//总成绩
};
vector<people> sage;//圣人
vector<people> nobleman;//君子
vector<people> smallman;//小人
vector<people> foolman;//愚人
int n;
int ans=0;//有效人数
int low,high;
bool cmp(people a,people b){
if(a.total>b.total){
return true;
}else if(a.total==b.total){
if(a.v_grade>b.v_grade){
return true;
}else if(a.v_grade==b.v_grade){
return a.id<b.id;
}
}
return false;
}
int main(){
cin>>n>>low>>high;
for(int i=0;i<n;i++){
people temp;
cin>>temp.id>>temp.v_grade>>temp.t_grade;
temp.total=temp.v_grade+temp.t_grade;
int v=temp.v_grade,t=temp.t_grade;
if(v<low||t<low){
continue;
}else if(v>=high&&t>=high){
ans++;
sage.push_back(temp);
}else if(v>=high&&t<high){
ans++;
nobleman.push_back(temp);
}else if(v<high&&t<high&&v>=t){
ans++;
foolman.push_back(temp);
}else{
ans++;
smallman.push_back(temp);
}
}
sort(sage.begin(),sage.end(),cmp);
sort(nobleman.begin(),nobleman.end(),cmp);
sort(foolman.begin(),foolman.end(),cmp);
sort(smallman.begin(),smallman.end(),cmp);
cout<<ans<<endl;
for(int i=0;i<sage.size();i++){
cout<<sage[i].id<<' '<<sage[i].v_grade<<' '<<sage[i].t_grade<<endl;
}
for(int i=0;i<nobleman.size();i++){
cout<<nobleman[i].id<<' '<<nobleman[i].v_grade<<' '<<nobleman[i].t_grade<<endl;
}
for(int i=0;i<foolman.size();i++){
cout<<foolman[i].id<<' '<<foolman[i].v_grade<<' '<<foolman[i].t_grade<<endl;
}
for(int i=0;i<smallman.size();i++){
cout<<smallman[i].id<<' '<<smallman[i].v_grade<<' '<<smallman[i].t_grade<<endl;
}
return 0;
}
标签:Talent,temp,grade,1062,high,int,Virtue,total,cmp 来源: https://blog.csdn.net/weixin_44123362/article/details/100181501