compare函数刚开始因为reutrn false写错了位置,在有些情况下可能不能返回值,发生了段错误。修正后的代码如下
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
struct student{
int id;
int cai;
int de;
int total;
};
bool compare(student a, student b){
if(a.total>b.total)
return true;
else if(a.total == b.total) {
if(a.de>b.de)
return true;
else if(a.de == b.de){
if(a.id<b.id)
return true;
}
}
return false;
}
int main(){
student tmp ;
vector<student> v1,v2,v3,v4;
int n,l,h,counter=0;
scanf("%d %d %d",&n,&l,&h);
while(n--){
scanf("%d %d %d",&tmp.id,&tmp.de,&tmp.cai);
tmp.total = tmp.cai + tmp.de;
if(tmp.cai>=l&&tmp.de>=l){
counter++;
if(tmp.cai>=h&&tmp.de>=h)
v1.push_back(tmp);
else if(tmp.cai<h&&tmp.de>=h)
v2.push_back(tmp);
else if(tmp.de>=tmp.cai)
v3.push_back(tmp);
else v4.push_back(tmp);
}
}
printf("%d\n",counter);
sort(v1.begin(),v1.end(),compare);
sort(v2.begin(),v2.end(),compare);
sort(v3.begin(),v3.end(),compare);
sort(v4.begin(),v4.end(),compare);
vector<student>::iterator itr;
for(itr = v1.begin(); itr!=v1.end();itr++)
printf("%d %d %d\n",itr->id,itr->de,itr->cai);
for(itr = v2.begin(); itr!=v2.end();itr++)
printf("%d %d %d\n",itr->id,itr->de,itr->cai);
for(itr = v3.begin(); itr!=v3.end();itr++)
printf("%d %d %d\n",itr->id,itr->de,itr->cai);
for(itr = v4.begin(); itr!=v4.end();itr++)
printf("%d %d %d\n",itr->id,itr->de,itr->cai);
return 0;
}