正解

nztao  •  11个月前


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// 学生结构体
struct Student {
    int id;       // 学号
    int total;    // 总分
    int chinese;  // 语文成绩
    int math;     // 数学成绩
    int english;  // 英语成绩
};

// 比较函数,用于排序
bool compare(const Student& a, const Student& b) {
    if (a.total != b.total) {
        return a.total > b.total;   // 按总分从高到低排序
    } else if (a.chinese != b.chinese) {
        return a.chinese > b.chinese;   // 总分相同,按语文成绩从高到低排序
    } else {
        return a.id < b.id;   // 总分和语文成绩都相同,按学号从小到大排序
    }
}

int main() {
    int n;
    cin >> n;

    vector<Student> students(n);
    for (int i = 0; i < n; i++) {
        cin >> students[i].chinese >> students[i].math >> students[i].english;
        students[i].id = i + 1;
        students[i].total = students[i].chinese + students[i].math + students[i].english;
    }

    sort(students.begin(), students.end(), compare);

    for (int i = 0; i < min(5, n); i++) {
        cout << students[i].id << " " << students[i].total << endl;
    }

    return 0;
}


评论:

请先登录,才能进行评论