罗小白


私信TA

用户名:Timmmmy

访问量:16398

签 名:

隔一年又回来刷题了...

等  级
排  名 141
经  验 7194
参赛次数 0
文章发表 46
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

有问题可以互相交流,共同提高 欢迎私信,请多指教:)

解题思路:
        排序函数 cmpP
注意事项:
        见注释
参考代码:

#include<bits/stdc++.h>
#define RATE 1.5        // 通过比率,1.5
using namespace std;

class P
{
public:
    int id;            // 报名号
    int score;         // 分数
    P() { }
    ~P() { }
};

// 分数高的排前面,分数一致时,报名号小的排前面
bool cmpP(P a, P b)
{
    if (a.score == b.score) {
        return a.id < b.id;
    }
    else {
        return a.score > b.score;
    }
}

int main()
{
    int n, m;
    while (cin >> n >> m) {
        int borderline;            // 分数线
        int passCount = 0;         // >= 分数线的人数
        P * Person = new P[n];     // 动态分配内存
        
        // 输入
        for (int i = 0; i < n; ++i) {
            cin >> Person[i].id >> Person[i].score;
        }
        // 排序
        sort(Person, Person + n, cmpP);
        // 分数线,强制类型转换到int即下取整
        // -1是因为:排名第1的人,在Person数组中下标为0(以此类推)
        borderline = Person[(int)(m * RATE) - 1].score;
        
        // 统计通过人数
        for (int i = 0; i < n; ++i) {
            if (Person[i].score >= borderline) passCount++;
            else break;
        }
        // 输出
        cout << borderline << " " << passCount << endl;
        for (int i = 0; i < passCount; ++i) {
            cout << Person[i].id << " " << Person[i].score << endl;
        }
        // 收回内存
        delete[] Person;
    }

    return 0;
}


 

0.0分

17 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区