求帮忙找不同,用第一个函数满分,第二个50分,哪里不同呀
查看原帖
求帮忙找不同,用第一个函数满分,第二个50分,哪里不同呀
1031598
KANGAR00楼主2023/7/8 10:39
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;
	}
}
2023/7/8 10:39
加载中...