90分求助,sum已初始-1,分相同输出第一个

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

UN_FIN @ 2024-10-12 18:19:13

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int getGrades(char *str){
    int grade_chn,grade_math,grade_eng;
    char str1[100000];
    char str2[100000];
    char str3[100000];
    int i = 0;
    int j = 0;
    sscanf(str, "%s %s %s", str1, str2, str3);
    grade_chn = atoi(str1);
    grade_math = atoi(str2);
    grade_eng = atoi(str3);
    int sum;
    sum = grade_chn + grade_math + grade_eng;
    return sum;
}
int main(){
    char str[100000];
    int n;
    char str1[100000];
    char str2[100000];
    char str3[100000];
    int max=-1;
    int j=0;
    scanf("%d", &n);
    getchar();
    for(int k=0;k<n;k++){
        fgets(str, sizeof(str), stdin);
        str[strcspn(str,"\n")] = '\0';
        int i;
        for(i=0;str[i]!=' ';i++){
            str1[i] = str[i];
        }
        str1[i] = '\0';
        for(i=i+1;str[i]!='\0';i++){
            str2[j] = str[i];
            j++;
        }
        str2[j] = '\0';
        if(getGrades(str2) > max) {
            max = getGrades(str2);
            strcpy(str3, str);
        }
        j = 0;
    }
    printf("%s", str3);
    return 0;
}

|