为什么第一个数据re了,其他都过了

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

gunners_in_code @ 2023-10-30 09:13:23

#include<bits/stdc++.h>
using namespace std;
struct student{
    string name;
    int chinese;
    int math;
    int english;
};
int main(){
    int n;
    cin>>n;
    struct student arr[n];
    int max=0,maxnum;
    for(int i=0;i<n;i++){
        cin>>arr[i].name>>arr[i].chinese>>arr[i].math>>arr[i].english;
        if(arr[i].chinese+arr[i].english+arr[i].math>max){
            max=arr[i].chinese+arr[i].english+arr[i].math;
            maxnum=i;
        }
    }
    cout<<arr[maxnum].name<<" "<<arr[maxnum].chinese<<" "<<arr[maxnum].math<<" "<<arr[maxnum].english;
    return 0;
} 

by Terrible @ 2023-10-30 10:05:41

@soyoanon 看来第一个数据中所有人都是 0 分,所以应该改成 max=-1


by gunners_in_code @ 2023-10-30 15:36:22

过了谢谢大佬


|