1 条题解

  • 0
    @ 2023-3-8 15:46:21

    题目要求我们按照成绩从高到低对成绩单进行排序,并且对于相同分数的学生按照名字的字典序小的在前排序。可以考虑使用结构体来存储每个学生的信息,然后使用 冒泡排序按照 成绩从高到低,成绩相同时按照名字的字典序小的在前的规则来排序;

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    const int N = 20;
    
    // 定义结构体表示学生信息
    struct Student {
        char name[N]; // 姓名
        int score; // 成绩
    };
    
    int main() {
        int n;
        cin >> n;
    
        Student students[n]; // 存储所有学生信息
        for (int i = 0; i < n; i++) {
            cin >> students[i].name >> students[i].score;
        }
    
        // 冒泡排序
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (students[j].score < students[j + 1].score || (students[j].score == students[j + 1].score && strcmp(students[j].name, students[j + 1].name) > 0)) {
                    swap(students[j], students[j + 1]);
                }
            }
        }
    
        for (int i = 0; i < n; i++) { // 输出排序后的结果
            cout << students[i].name << " " << students[i].score << endl;
        }
    
        return 0;
    }
    
    
    • 1

    信息

    ID
    303
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    (无)
    递交数
    8
    已通过
    2
    上传者