求助求助 /哭

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

fanchenice @ 2024-03-20 11:37:09

死活二十分了 /哭

#include <iostream>
using namespace std;

struct Student
{
    string Name;
    int Chinese = 0 , Math = 0 , English = 0 ;
    int Total = Chinese + Math + English ;

} s[1100] ;

int main(int argc, const char * argv[]) {

    int N = 0 , Max = 0 , MaxNum = 0 ;
    cin >> N ;
    for ( int i = 0 ; i < N ; i++ )
    {
        cin >> s[i].Name >> s[i].Chinese >> s[i].Math >> s[i].English ;
    }
    cout << 1 << endl ;
    for ( int i = 0 ; i < N ; i++ )
    {
        if ( s[i].Total > MaxNum )
        {
            Max = i ;
            MaxNum = s[i].Total ;
        }
    }
    cout << s[Max].Name << " " << s[Max].Chinese << " " << s[Max].Math << " " << s[Max].English <<endl ;
    return 0;
}

by loser_wanghan @ 2024-03-20 11:55:47

@fanchenice Student结构体有问题,在结构体里是先运行 int Chinese = 0 , Math = 0 , English = 0 ; 再运行 int Total = Chinese + Math + English ; 所以 Total 一直等于 0

应该为:

#include <iostream>
using namespace std;

struct Student
{
    string Name;
    int Chinese , Math , English ;
    int Total ;

} s[1100] ;

int main(int argc, const char * argv[]) {

    int N = 0 , Max = 0 , MaxNum = 0 ;
    cin >> N ;
    for ( int i = 0 ; i < N ; i++ )
    {
        cin >> s[i].Name >> s[i].Chinese >> s[i].Math >> s[i].English ;
        s[i].Total=s[i].Chinese+s[i].Math+s[i].English;
    }
    for ( int i = 0 ; i < N ; i++ )
    {
        if ( s[i].Total > MaxNum )
        {
            Max = i ;
            MaxNum = s[i].Total ;
        }
    }
    cout << s[Max].Name << " " << s[Max].Chinese << " " << s[Max].Math << " " << s[Max].English <<endl ;
    return 0;
}

by fanchenice @ 2024-03-20 15:51:52

@loser_wanghan AC了 谢谢大佬!!!!


|