没分蒟蒻求助

P1093 [NOIP2007 普及组] 奖学金

kkkSb_O3 @ 2023-11-19 14:27:16

#include<bits/stdc++.h>
using namespace std;

int n;

struct  qqq
{
    int y,s,e,z,id;
}a[303];

bool cmp(qqq x,qqq y)
{
    if(x.z!=y.z)
        return x.z>y.z;
    else if (x.y!=y.y)
        return x.y>y.y;
    else
        return x.id<y.id;

}

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].y>>a[i].s>>a[i].e;
        a[i].id=i;
        a[i].z=a[i].y+a[i].s+a[i].e;
    }
    sort(a+1,a+n,cmp);
    for(int i=1;i<=5;i++)
    {
        cout<<a[i].id<<' '<<a[i].z<<"\n";
    }
}

by Lyrith_with_xQ @ 2023-11-19 14:30:42

你代码中的sort(a+1,a+n,cmp);应改为sort(a+1,a+n+1,cmp);


by lzm0107 @ 2023-11-19 14:32:47

#include<bits/stdc++.h>
using namespace std;

int n;

struct  qqq
{
    int y,s,e,z,id;
}a[303];

bool cmp(qqq x,qqq y)
{
    if(x.z!=y.z)
        return x.z>y.z;
    else if (x.y!=y.y)
        return x.y>y.y;
    else
        return x.id<y.id;

}

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].y>>a[i].s>>a[i].e;
        a[i].id=i;
        a[i].z=a[i].y+a[i].s+a[i].e;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=5;i++)
    {
        cout<<a[i].id<<' '<<a[i].z<<"\n";
    }
}

|