为啥第一个测试点过不了?求大佬帮助!

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

LiXinLong_LXL @ 2024-05-08 22:12:59

#include<bits/stdc++.h>
using namespace std;
struct stu{
    string name;
    int a,b,c;
    int sum=0;
}p[100000];
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>p[i].name>>p[i].a>>p[i].b>>p[i].c;
        p[i].sum+=p[i].a+p[i].b+p[i].c;
    }
    int id=0,sum1=0;
    for(int i=1;i<=n;i++){
        if(p[i].sum>sum1){
            id=i;
            sum1=p[i].sum;
        }
    }
    cout<<p[id].name<<" "<<p[id].a<<" "<<p[id].b<<" "<<p[id].c;
    return 0;
}

by calms @ 2024-05-08 22:17:13

@LiXinLong_LXL 你的sum1初始值不能为0,因为总分有可能是0.


#include<bits/stdc++.h>
using namespace std;
struct stu{
    string name;
    int a,b,c;
    int sum=0;
}p[100000];
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>p[i].name>>p[i].a>>p[i].b>>p[i].c;
        p[i].sum+=p[i].a+p[i].b+p[i].c;
    }
    int id=0,sum1=-1;
    for(int i=1;i<=n;i++){
        if(p[i].sum>sum1){
            id=i;
            sum1=p[i].sum;
        }
    }
    cout<<p[id].name<<" "<<p[id].a<<" "<<p[id].b<<" "<<p[id].c;
    return 0;
}

by Norsuman371 @ 2024-05-08 22:19:58

楼上正解


by LiXinLong_LXL @ 2024-05-08 22:22:50

@calms 明白了明白了,感谢您的指导!!!已经AC,谢谢!


|