第一个点WA了,求助大佬

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

chenmumin @ 2023-07-26 12:31:33

#include<bits/stdc++.h>
using namespace std;
struct student{
    string name;
    int Chinese,math,English;
}a,ans;
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a.name>>a.Chinese>>a.math>>a.English;
        if(a.Chinese+a.English+a.math>ans.Chinese+ans.English+ans.math){
            ans=a;
        }
    }
    cout<<ans.name<<" "<<ans.Chinese<<" "<<ans.math<<" "<<ans.English;
}

by FanMingxuan @ 2023-07-26 12:59:58

struct里面需要初始值,不然ans是没有初始值的

修改后:


#include<bits/stdc++.h>
using namespace std;
struct student{
    string name;
    int Chinese = -1,math = -1,English = -1;
}a,ans;
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a.name>>a.Chinese>>a.math>>a.English;
        if(a.Chinese+a.English+a.math>ans.Chinese+ans.English+ans.math){
            ans=a;
        }
    }
    cout<<ans.name<<" "<<ans.Chinese<<" "<<ans.math<<" "<<ans.English;
}

by chenmumin @ 2023-07-26 14:57:35

@FanMingxuan AC了谢谢


|