80分求助

P1093 [NOIP2007 普及组] 奖学金

Mizuiro79 @ 2022-01-20 10:25:30

#include<bits/stdc++.h>
using namespace std;
struct student{
    int xh;
    int yw;
    int sx;
    int yy;
    int zf;
}a[400];
int n;
bool comp1(student x,student y)
{
    return x.yw>y.yw;
}
bool comp2(student x,student y)
{
    return x.xh>y.xh;
}
bool comp3(student x,student y)
{
    return x.zf>y.zf;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].yw>>a[i].sx>>a[i].yy;
        a[i].zf=a[i].yw+a[i].sx+a[i].yy;
        a[i].xh=i;
    }
    sort(a+1,a+n+1,comp3);
    for(int i=1;i<=n;i++)
    {
        if(a[i].zf==a[i+1].zf)
        {
            if(a[i].yw<a[i+1].yw)
            {
                swap(a[i].xh,a[i+1].xh);
            }   
        }
    }
    for(int i=1;i<=5;i++)
    {
        cout<<a[i].xh<<" "<<a[i].zf<<endl;
    }
    return 0;
}

by maxi_marvel @ 2022-01-20 10:46:53

您的排序有问题,你可已您的comp3改成:

bool comp3(student x,student y)
{
    return x.zf==y.zf?(x.yw==y.yw?(x.xh<y.xh):x.yw>y.yw):x.zf>y.zf;
}

可以了解一下三目运算符号,然后您的for就可以删掉了,您的for只能判断相邻的两位的,是不正确的。


by maxi_marvel @ 2022-01-20 10:47:05

@Awaki22


by arrow_king @ 2022-01-20 10:48:36

@Awaki22 sort函数,你值得拥有


by Mizuiro79 @ 2022-01-20 11:05:51

@maxi_marvel 好的,感谢qwq


|