(零分求助)发奋苦学c++,归来普及减仍全WA

P1093 [NOIP2007 普及组] 奖学金

yoyoSGH @ 2024-10-20 11:19:43

不知道该说啥(样例全都过了)

#include<bits/stdc++.h>
using namespace std;
struct Student{
    int num,a,b,c,sum;
};
bool cmp(Student x,Student y)
{
    if(x.sum==y.sum&&x.a==y.a) return x.num>y.num;
    else if(x.sum==y.sum) return x.a>y.a;
    else return x.sum>y.sum; 
} 
int main() {
    Student s[305];
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        s[i].num=i;
        cin>>s[i].a>>s[i].b>>s[i].c;
        s[i].sum=s[i].a+s[i].b+s[i].c;
    }
    sort(s+0,s+n+1,cmp);
    for(int i=1;i<=5;i++) cout<<s[i].num<<' '<<s[i].sum<<endl;
    return 0;
}

用的是sort函数,不知道错在哪儿


by mainxy @ 2024-10-20 11:25:14

bool cmp()-->写少了


by ridewind2013 @ 2024-10-20 11:25:48

应该是:

sort(s+1,s+n+1,cmp)

by ridewind2013 @ 2024-10-20 11:27:18

@yoyoSGH


by mainxy @ 2024-10-20 11:28:45

#include<bits/stdc++.h>
using namespace std;
struct stu{
    int allscore,chi,eng,maths,id;
};
stu a[311];
bool cmp(stu a,stu b){
    if(a.allscore>b.allscore) return true;
    else if(a.allscore<b.allscore) return false;
    else{
        if(a.chi>b.chi) return true;
        else if(a.chi<b.chi) return false;
        else{
            if(a.id<b.id) return true;
            else return false;
        }
    }
}
int n;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].chi>>a[i].maths>>a[i].eng;
        a[i].allscore=a[i].chi+a[i].maths+a[i].eng;
        a[i].id=i;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=5;i++) cout<<a[i].id<<' '<<a[i].allscore<<endl;
    return 0;
}

by claoday @ 2024-10-20 11:30:15

你的 sort 排序有问题,应该为:

sort(s+1,s+n+1,cmp)

这样就有 90pts 了。记录


by claoday @ 2024-10-20 11:31:31

然后再把 cmp 函数改一下:

    if(pre.b!=next.b)
    return pre.b>next.b;
    else if(pre.b==next.b)
    {
        if(pre.c==next.c)
        return pre.xh<next.xh;
        else
        return pre.c>next.c;
    }

求关


by claoday @ 2024-10-20 11:34:15

@yoyoSGH


by yoyoSGH @ 2024-10-21 13:55:46

蟹蟹泥们!!!


|