1 条题解

  • 1
    @ 2024-6-12 23:17:23

    【题目大意】给定N个同学的语数英成绩,按照题目的要求对学生求解排名。

    【考纲知识点】排序算法

    【解题思路】该题目做法多样,可用结构体/pair数组/truple等

    总体流程:输入人数和对应的成绩,注意此时需要将人员的编号也存储下来,接下来按照比较优先级对人员进行排名:

    优先级从高到低依次为:总分、语文数学总分、语文或数学最高分大的优先。

    可以考虑用truple,将对应的优先级数值存储后,做一次sort降序即可。

    【参考程序】

    #include <iostream>
    #include <algorithm>
    #include <tuple>
    using namespace std;
    const int MAX_N = 10005;
    tuple<int, int, int, int> students[MAX_N];
    int main()
    {
        ios::sync_with_stdio(false);
        int N;
        cin >> N;
        for (int i = 0; i < N; ++i)
        {
            int c, m, e;
            cin >> c >> m >> e;
            students[i] = make_tuple(c + m + e, c + m, max(c, m), i);
        }
        sort(students, students + N, greater<tuple<int, int, int, int>>());
        int rank[N];
        int curr_rank;
        tuple<int, int, int> last_student = make_tuple(-1, -1, -1);
        for (int i = 0; i < N; ++i)
        {
            if (make_tuple(get<0>(students[i]), get<1>(students[i]), get<2> (students[i])) != last_student)
            {
                last_student = make_tuple(get<0>(students[i]), get<1>(students[i]), get<2>(students[i]));
                curr_rank = i + 1;
            }
            rank[get<3>(students[i])] = curr_rank;
        }
        for (int i = 0; i < N; ++i)
        {
            cout << rank[i] << endl;
        }
        return 0;
    }
    
    • 1

    [GESP202403 五级] 成绩排序

    信息

    ID
    648
    时间
    1000ms
    内存
    512MiB
    难度
    5
    标签
    递交数
    29
    已通过
    14
    上传者