1 条题解

  • 0
    @ 2025-1-12 16:13:21
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    struct Student {
        string name;
        int chinese;
        int math;
        int english;
        int science;
        int total;
        Student(string n, int c, int m, int e, int s) 
            : name(n), chinese(c), math(m), english(e), science(s) {
            total = c + m + e + s; 
        }
    };
    bool compareByChinese(const Student &a, const Student &b) {
        if (a.chinese != b.chinese) return a.chinese > b.chinese; 
        return a.name < b.name; 
    }
    
    bool compareByMath(const Student &a, const Student &b) {
        if (a.math != b.math) return a.math > b.math;
        return a.name < b.name;
    }
    
    bool compareByEnglish(const Student &a, const Student &b) {
        if (a.english != b.english) return a.english > b.english;
        return a.name < b.name;
    }
    
    bool compareByScience(const Student &a, const Student &b) {
        if (a.science != b.science) return a.science > b.science;
        return a.name < b.name;
    }
    
    bool compareByTotal(const Student &a, const Student &b) {
        if (a.total != b.total) return a.total > b.total;
        return a.name < b.name;
    }
    
    int main() {
        int n; 
        cin >> n; 
        vector<Student> students;
        for (int i = 0; i < n; ++i) {
            string name;
            int chinese, math, english, science;
            cin >> name >> chinese >> math >> english >> science;
            students.emplace_back(name, chinese, math, english, science);
        }
        sort(students.begin(), students.end(), compareByChinese);
        for (int i = 0; i < 4; ++i) {
            cout << students[i].name << (i < 3 ? " " : "\n"); 
        }
        sort(students.begin(), students.end(), compareByMath);
        for (int i = 0; i < 4; ++i) {
            cout << students[i].name << (i < 3 ? " " : "\n"); 
        }
        sort(students.begin(), students.end(), compareByEnglish);
        for (int i = 0; i < 4; ++i) {
            cout << students[i].name << (i < 3 ? " " : "\n"); 
        }
        sort(students.begin(), students.end(), compareByScience);
        for (int i = 0; i < 4; ++i) {
            cout << students[i].name << (i < 3 ? " " : "\n"); 
        }
        sort(students.begin(), students.end(), compareByTotal);
        for (int i = 0; i < 4; ++i) {
            cout << students[i].name << (i < 3 ? " " : "\n"); 
        }
    
        return 0;
    }
    

    信息

    ID
    121
    时间
    1000ms
    内存
    256MiB
    难度
    6
    标签
    (无)
    递交数
    156
    已通过
    44
    上传者