50分,用的结构体,哪里错了呢,求大佬5555

P1093 [NOIP2007 普及组] 奖学金

qwe1471900575 @ 2021-03-04 01:25:18

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
struct student
{
    int id;
    int chinese;
    int sum;
};
int cmp(student a, student b)//******
{
    return a.sum > b.sum;
    return a.chinese > b.chinese;
    return a.id < b.id;
}
student a[350];
int main()
{
    freopen("sb.in", "r", stdin);
    freopen("sb.out", "w", stdout);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int math, english;
        cin >> a[i].chinese >> math >> english;
        a[i].sum = math + english + a[i].chinese;
        a[i].id = i + 1;
    }
    sort(a, a + n, cmp);
    for (int i = 0; i < 5; i++)
    {
        cout << a[i].id << " " << a[i].sum << endl;
    }
    return 0;
}

by One_Zzz6174 @ 2021-03-04 07:40:42

cmp$函数里如果$a.sum$ == $b.sum$,在第一个$return$就会返回$false$,而不会进入下一个$return

by One_Zzz6174 @ 2021-03-04 07:42:18

return a.sum != b.sum ? a.sum > b.sum : a.chinese != b.chinese ? a.chinese > b.chinese : a.id < b.id

by qwe1471900575 @ 2021-03-04 18:30:42

@One_Zzz

AC了谢谢


|