90分,哪里错了???找了半天

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

Charlie_zs @ 2023-11-19 15:36:52

#include <bits/stdc++.h>
using namespace std ;

int n ;

struct student
{
    string name ;
    int english , chinese , math , num , id ;
} ;

student a[1005] ;

bool cmp( student a , student b )
{
    return a.num > b.num ;
}

int main()
{
    cin >> n ;
    for( int i = 1 ; i <= n ; i ++ )
    {
        cin >> a[i].name >> a[i].chinese >> a[i].math >> a[i].english ;
        a[i].num = a[i].chinese + a[i].math + a[i].english ;
        a[i].id = i ;
    }
    sort( a + 1 , a + n + 1 , cmp ) ;
    cout << a[1].name << " " << a[1].chinese << " " << a[1].math << " " << a[1].english ;
    return 0 ;
 }

所以说哪儿错了呢???T_T 大佬们QAQ


by heyx0201 @ 2023-11-19 15:42:49

@Charlie_zs 比较器改一下:

bool cmp( student a , student b )
{
    return a.num > b.num || (a.num == b.num && a.id < b.id);
}

by Charlie_zs @ 2023-11-19 15:49:10

感谢大佬!!!


|