90求调

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

DKsniper @ 2024-01-24 11:22:58

#include<bits/stdc++.h>
using namespace std;
int n;
struct l
{
    string name;
    int yw,sx,yy,ans;
}a[1001];
int cmp(l x,l y)
{
    return x.ans>y.ans;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].name>>a[i].yw>>a[i].sx>>a[i].yy;
        a[i].ans=a[i].yw+a[i].sx+a[i].yy;
    }
    sort(a+1,a+1+n,cmp);
    cout<<a[1].name<<" "<<a[1].yw<<" "<<a[1].sx<<" "<<a[1].yy;
    return 0;
}

by Eternity_Yoke @ 2024-01-24 11:29:30

#include<bits/stdc++.h>
using namespace std;
int n;
struct l
{
    string name;
    int yw,sx,yy,ans,pos;
}a[1001];
int cmp(l x,l y)
{   if(x.ans==y.ans)return x.pos<y.pos;
    return x.ans>y.ans;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].name>>a[i].yw>>a[i].sx>>a[i].yy;
        a[i].ans=a[i].yw+a[i].sx+a[i].yy;
        a[i].pos=i;
    }
    sort(a+1,a+1+n,cmp);
    cout<<a[1].name<<" "<<a[1].yw<<" "<<a[1].sx<<" "<<a[1].yy;
    return 0;
}

@DKsniper 加个pos记录位置,ans 相同输出靠前的


|