90分求助

P1093 [NOIP2007 普及组] 奖学金

zhangyan8216 @ 2023-11-25 21:50:30

#include <bits/stdc++.h>
using namespace std;
int n, ans;

struct grade {
    int number, chinese, match, english;
} a[310];

bool cmp(grade a, grade b) {
    int tmp1 = a.chinese + a.english + a.match;
    int tmp2 = b.chinese + b.english + b.match;
    if (tmp1 == tmp2)
        if (a.chinese != b.chinese)
            return a.chinese > b.chinese;
        else
            return a.number > b.number;
    return tmp1 > tmp2;

}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        a[i].number = i + 1;
        cin >> a[i].chinese >> a[i].match >> a[i].english;
    }
    sort(a, a + n, cmp);
    for (int i = 0; i < 5; i++) {
        cout << a[i].number << ' ' << a[i].chinese + a[i].english + a[i].match << endl;
    }

    return 0;
}

by hetong321 @ 2023-11-25 21:51:44

哪道题?


by Wyy20120223 @ 2023-11-25 22:05:36

题目


by Wyy20120223 @ 2023-11-25 22:20:58

@zhangyan8216 你的问题出在这里:


else
    return a.number > b.number;
这里应该把">"号改成"<"号,因为题目中说:
如果两个同学总分和语文成绩都相同,那么规定学号小的同学 排在前面,这样,每个学生的排序是唯一确定的。

by Wyy20120223 @ 2023-11-25 22:23:17

@zhangyan8216


by zhangyan8216 @ 2023-11-25 22:43:01

@Wyy20120223 谢谢


by zhangyan8216 @ 2023-11-25 22:43:59

@hetong321 解决了谢谢


|