求助大佬!!!!!!

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

xugaoyi1 @ 2024-04-28 16:18:27

0分,怎么改?

#include<bits/stdc++.h>
using namespace std;
struct student
{
    string name;
    int a,b,c;
    int sum;
};
int cmp(student a,student b){
    return a.sum>b.sum;
}
student box[1000];
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>box[i].name>>box[i].b>>box[i].a>>box[i].c;
        box[i].sum=box[i].a+box[i].b+box[i].c;
    }
    sort(box+1,box+n+1,cmp);
    cout<<box[1].name<<" "<<box[1].b<<" "<<box[1].c<<endl;
    return 0;
}

by FXLIR @ 2024-04-28 16:29:16

@xugaoyi1

#include<bits/stdc++.h>
using namespace std;
struct student
{
    string name;
    int a,b,c;
    int id;//处理多个最高总分的情况 
    int sum;
};
bool cmp(student a,student b){
    if(a.sum!=b.sum){
        return a.sum>b.sum;
    }else{
        return a.id<b.id;
    }//总分不同比总分,总分相同比 id 
}
student box[1005];//防止数组越界 
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>box[i].name>>box[i].b>>box[i].a>>box[i].c;
        box[i].id=i;//id 为输入顺序 
        box[i].sum=box[i].a+box[i].b+box[i].c;
    }
    sort(box+1,box+n+1,cmp);
    cout<<box[1].name<<" "<<box[1].b<<" "<<box[1].a<<" "<<box[1].c<<endl;//缺少输出 
    return 0;
}

by FXLIR @ 2024-04-28 16:30:10

不得不说你这问题是真多……

连输入顺序似乎都错了……

还有,你连样例都不测的吗?


by Junior_Young @ 2024-04-28 16:34:29

不要不调试就将代码交上去

第21行,低级错误

cout<<box[1].name<<" "<<box[1].b<<" "<<box[1].c<<endl;
cout<<box[1].name<<" "<<box[1].b<<" "<<box[1].a<<" "<<box[1].c<<endl;

by Junior_Young @ 2024-04-28 16:36:52

@xugaoyi1 顺便关注我,thx.


by AAAuc03 @ 2024-04-28 17:13:33

不0分才怪


|