90分求助

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

wangqianyi @ 2022-04-01 15:22:28

#include<bits/stdc++.h>
using namespace std;
struct Student {
    string Name;
    int ab;
    int bc;
    int cd;
}a,ans;
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a.Name>>a.ab>>a.bc>>a.cd;
        if(a.ab+a.bc+a.cd>ans.ab+ans.bc+ans.cd){
            ans=a;
        }
    }
    cout <<ans.Name<<" "<<ans.ab<<" "<<ans.bc<<" "<<ans.cd<<endl;
    return 0;
}

by zxy123bc @ 2022-04-01 15:25:05

@wangqianyi

ans初值赋为-1


by zxy123bc @ 2022-04-01 15:25:45

@wangqianyi ans直接赋为第一个学生也行


by wangqianyi @ 2022-04-01 15:31:37


@[zxy123bc](/user/550357) ans不是int类型的吗,怎么初值赋为-1

by the_Soviet_Union @ 2022-04-01 15:33:09

@wangqianyi ans=-1;


by wangqianyi @ 2022-04-01 15:34:52

@the_Soviet_Union 编译失败


by zxy123bc @ 2022-04-01 15:35:38

@wangqianyi 我的意思是给他个初值,要不然成绩全是ans没法更新


by wangqianyi @ 2022-04-01 15:37:55

@zxy123bc 能给我改一下吗,我不是很明白


by zxy123bc @ 2022-04-01 15:40:43

@wangqianyi

#include<bits/stdc++.h>
using namespace std;
struct Student {
    string Name;
    int ab;
    int bc;
    int cd;
}a,ans;
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a.Name>>a.ab>>a.bc>>a.cd;
        if(i==1){
            ans=a;
        } 
        if(a.ab+a.bc+a.cd>ans.ab+ans.bc+ans.cd){
            ans=a;
        }
    }
    cout <<ans.Name<<" "<<ans.ab<<" "<<ans.bc<<" "<<ans.cd<<endl;
    return 0;
}

by zxy123bc @ 2022-04-01 15:41:13

@wangqianyi 这是最简单的改法


by wangqianyi @ 2022-04-01 15:41:29

@zxy123bc 好的,十分感谢


|