请一位大神来解答这个问题

有兽焉 0 Reputation points
2023-10-02T14:32:32.2933333+00:00

描述

世博会志愿者的选拔工作正在 A 市如火如荼的进行。为了选拔最合适的人才,A 市对所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试。面试分数线根据计划录取人数的 150% 划定,即如果计划录取 M 名志愿者,则面试分数线为排名第 M ×150%(向下取整)名的选手的分数,而最终进入面试的选手为笔试成绩不低于面试分数线的所有选手。

现在就请你编写程序划定面试分数线,并输出所有进入面试的选手的报名号和笔试成绩。

输入描述

第一行,两个整数 n , M (5≤ n ≤5000,3≤ Mn ),中间用一个空格隔开,其中 n 表示报名参加笔试的选手总数, M 表示计划录取的志愿者人数。输入数据保证 M ×150% 向下取整后小于等于 n

第二行到第 n +1 行,每行包括两个整数,中间用一个空格隔开,分别是选手的报名号 k (1000≤ k ≤9999)和该选手的笔试成绩 s (1≤ s ≤100)。数据保证选手的报名号各不相同。

输出描述

第一行,有 2 个整数,用一个空格隔开,第一个整数表示面试分数线;第二个整数为进入面试的选手的实际人数。

从第二行开始,每行包含 2 个整数,中间用一个空格隔开,分别表示进入面试的选手的报名号和笔试成绩,按照笔试成绩从高到低输出,如果成绩相同,则按报名号由小到大的顺序输出。

用例输入 1

6 3 
1000 90 
3239 88 
2390 95 
7231 84 
1005 95 
1001 88

用 例输 出 1

88 5 
1005 95 
2390 95 
1000 90 
1001 88 
3239 88
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,933 questions
{count} votes

1 answer

Sort by: Most helpful
  1. YujianYao-MSFT 4,291 Reputation points Microsoft External Staff
    2023-10-03T01:50:32.3266667+00:00

    Hi 有兽焉

    You could refer to this sample:

    The following code uses pair to bind the registration number and results, and the vector is used as an array to save this information. The sort function is used to sort the vector, and the subsequent code outputs information such as score lines as required.

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main() {
        int n, M;
        cin >> n >> M;
    
        vector<pair<int, int>> scores;  // store number and score
    
        for (int i = 0; i < n; i++) {
            int k, s;
            cin >> k >> s;
            scores.push_back(make_pair(k, s));
        }
    
        // sort
        sort(scores.begin(), scores.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
            if (a.second == b.second) {
                return a.first < b.first;
            }
            return a.second > b.second;
            });
    
        // calculate interview score
        int interview_cutoff = scores[M * 150 / 100 - 1].second;
    
        // output interview score and number of people
       // int result = static_cast<int>(floor(M * 1.5));
        int result = static_cast<int>(std::ceil(M * 1.5));
        cout << interview_cutoff << " " << result << endl;
    
        // output number and score
        for (int i = 0; i < result; i++) {
            cout << scores[i].first << " " << scores[i].second << endl;
        }
    
        return 0;
    }
    

    Result:

    User's image

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.