哪错了90分

P5740 【深基7.例9】最厉害的学生

1234o @ 2023-09-11 21:20:42

#include<bits/stdc++.h>
using namespace std;
struct Stu{
    string name;
    int s,b,y;
}a[1005]; 
bool cmp(Stu a,Stu b){
        return a.b+a.s+a.y>b.s+b.y+b.b;
}
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].name>>a[i].s>>a[i].b>>a[i].y;
    }
    sort(a+1,a+1+n,cmp);
    cout<<a[1].name<<' '<<a[1].s<<" "<<a[1].b<<" "<<a[1].y;
    return 0;
}

by LiAuPb @ 2023-09-11 21:24:43

总分相同呢?


by GPUawa @ 2023-09-11 22:07:26

可以特判错的那个点

提示:cmp函数


by 1234o @ 2023-09-12 16:47:51

哪怎吗做?


by AMDRYZEN @ 2023-09-22 20:34:34

可以不用排序,直接用找总分最大值


by AMDRYZEN @ 2023-09-22 20:36:18

@lxlrighl_lixiaolong

#include<bits/stdc++.h>
using namespace std;
struct student{
    string name;
    int yw,sx,yy,score,xh;
}a[1005];
bool cmp(student a,student b)
{
    if(a.score==b.score)
    return a.xh<b.xh;
    return a.score>b.score;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,i,max=INT_MIN,maxi,maxi2,maxi3;
    string maxi4;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i].name>>a[i].yw>>a[i].sx>>a[i].yy;
        a[i].score=a[i].sx+a[i].yw+a[i].yy;
        if(max<a[i].score)
        {
            max=a[i].score;
            maxi4=a[i].name;
            maxi2=a[i].yw;
            maxi3=a[i].sx;
            maxi=a[i].yy;
        }
    }
    cout<<maxi4<<" "<<maxi2<<" "<<maxi3<<" "<<maxi;
}

|