KANGAR00 @ 2023-07-08 10:39:07
bool cmp(student a,student b)
{
if(a.sum>b.sum) return 1;
else if(a.sum<b.sum) return 0;
else
{
if(a.chinese>b.chinese) return 1;
else if(a.chinese<b.chinese) return 0;
else
{
if(a.id>b.id) return 0;
else return 1;
}
}
}
bool cmp(student x,student y)
{
if(x.sum==y.sum)
{
if(x.chinese<y.chinese)
return x.sum<y.sum;
else if(x.chinese == y.chinese)
{
if(x.id>y.id)
return x.sum<y.sum;
}
return x.sum>y.sum;
}
return x.sum>y.sum;
}
----------------------------------------------
下面是代码,也就是得50分的,但是替换成第一个函数就满分了
#include<bits/stdc++.h>
using namespace std;
struct student
{
int id;
int chinese;
int math;
int english;
int sum;
};
bool cmp(student x,student y)
{
if(x.sum==y.sum)
{
if(x.chinese<y.chinese)
return x.sum<y.sum;
else if(x.chinese == y.chinese)
{
if(x.id>y.id)
return x.sum<y.sum;
}
return x.sum>y.sum;
}
return x.sum>y.sum;
}
int main()
{
int n;
int c,i;
cin>>n;
student *stu = new student[n+1];
for(i=1;i<=n;i++)
{
cin>>stu[i].chinese>>stu[i].math>>stu[i].english;
stu[i].sum = stu[i].chinese+stu[i].math+stu[i].english;
stu[i].id =i;
}
sort(stu+1,stu+n+1,cmp);
for(i=1;i<=5;i++)
{
cout<<stu[i].id<<" "<<stu[i].sum<<endl;
}
}
by Clever_Ivory @ 2023-07-08 11:00:21
bool cmp(student x,student y)
{
if(x.sum==y.sum)
{
if(x.chinese<y.chinese)
return x.sum<y.sum;
else if(x.chinese == y.chinese)
{
if(x.id>y.id)
return x.sum<y.sum;
}
return x.sum>y.sum; //这个地方应该是返回1的,但是你现在返回的是x.sum > y.sum,可是这个分支中x.sum == y.sum,所以返回的肯定是0啊。改成1就行了吧。
}
return x.sum>y.sum;
}
by KANGAR00 @ 2023-07-08 14:33:12
@jwang69 明白了,谢谢大佬