为啥RE+WA

P1093 [NOIP2007 普及组] 奖学金

abccc @ 2024-05-16 16:56:44

#include<bits/stdc++.h>
using namespace std;
struct student{
    int chinese,mathe,english,num=0,all=0;
};
bool cmp(student A,student B){
    if(A.all>B.all){
        return A.all>B.all;
    }
    else if(A.chinese>B.chinese){
        return A.chinese>B.chinese;
    }
    else{
        return A.num<B.num;
    }
}
student a[1000000];
int main(){
    int n;cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].chinese>>a[i].mathe>>a[i].english;
        a[i].num=i;
        a[i].all=a[i].chinese+a[i].mathe+a[i].english;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=5;i++){
        cout<<a[i].num<<" "<<a[i].all<<endl;
    }
}

by LHM_zs @ 2024-05-16 17:00:14

@abccc cmp没判断好


by kongjunwen @ 2024-05-16 20:02:22

cmp判断出错。

先看题目:比较是先比较总成绩,再比较语文,最后比较排名。(这有一点点问题~)

你的cmp判断A.all>B.all(同chinese),但是题目说如果一样就比下一个,所以cmp判断应改成A.all!=B.all(同chinese)

只需改判断条件,return不改

改了之后就没问题啦~

a数组只需开305……

求关

祝你早日AC


by zhuyucheng6046 @ 2024-06-01 19:27:41

正确代码:


#include<bits/stdc++.h>
using namespace std;
struct stu
{
    int chinese,math,english,zongfen,xuehao;
}student[1000];
bool cmp(stu a,stu b)
{
    if(a.zongfen>b.zongfen)return 1;
    else if(a.zongfen<b.zongfen)return 0;
    else
    {
        if(a.chinese>b.chinese)return 1;
        else if(a.chinese<b.chinese)return 0;
        else
        {
            if(a.xuehao>b.xuehao)return 0;
            else
            return 1;
        }
    }
}
int main()
{
    int n,s=1;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>student[i].chinese >>student[i].math >>student[i].english ;
        student[i].xuehao =s;s++;
        student[i].zongfen =student[i].chinese+student[i].math+student[i].english;
    }
    sort(student,student+n,cmp );
    for(int i=0;i<5;i++)
    {
        cout<<student[i].xuehao<<" "<<student[i].zongfen <<endl;
    }
    return 0;
}
求关注!

by caizimo @ 2024-06-18 22:00:23

666

好吧 虽然我会 dann我没AC


|