PAT乙级刷题/1015 德才论/C++实现

一、题目描述

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式:

输入第一行给出 3 个正整数,分别为:N(≤105),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。

随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。

输出格式:

输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60
//结尾无空行

输出样例:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90
//结尾无空行

二、思路分析

读题后,我们发现最基本的要求是德才分均需要达到及格分才能被录取,在这基础上,划分为四类考生。

第一类:德才分均不低于优先录取线

第二类:德分不低于优先录取线但才分未到

第三类:德才分均没到优先录取线但德分高于才分

第四类:其他剩余考生

明确以上分类后,可以建立四个结构动态数组,利用for循环和if-else条件控制语句,分别将四类考生的相关信息加入到数组里,利用sort()函数排序,输出即可。

三、题解代码以及提交截图

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct StuInfo
{
    int StuId;
    int DeScore;
    int CaiScore;
    int sum;
};

bool cmp(StuInfo a,StuInfo b);

int main()
{
    int N,H,L;
    cin >> N >> L >> H;
    vector<StuInfo> list_1;//存储器
    vector<StuInfo> list_2;
    vector<StuInfo> list_3;
    vector<StuInfo> list_4;
    for(int i = 0;i < N;i++){
        int StuId,DeScore,CaiSore;
        cin >> StuId >> DeScore >> CaiSore;
        StuInfo student{};
        if(DeScore >= L && CaiSore >= L){ //录取基本要求
            student.StuId = StuId;
            student.DeScore = DeScore;
            student.CaiScore = CaiSore;
            student.sum = DeScore + CaiSore;
            if(DeScore >= H && CaiSore >= H){  //第一类
                list_1.push_back(student);
            }
            else if(CaiSore < H && DeScore >= H){  //第二类
                list_2.push_back(student);
            }
            else if(CaiSore < H && DeScore < H && DeScore >= CaiSore){  //第三类
                list_3.push_back(student);
            }
            else{
                list_4.push_back(student);  //第四类
            }
        }

    }
    sort(list_1.begin(),list_1.end(),cmp); //按照cmp排序方式排序
    sort(list_2.begin(),list_2.end(),cmp);
    sort(list_3.begin(),list_3.end(),cmp);
    sort(list_4.begin(),list_4.end(),cmp);
    cout << list_1.size() + list_2.size() + list_3.size() + list_4.size() << endl;
   
 for(auto & j : list_1){ //输出
        cout << j.StuId
             << " "
             << j.DeScore
             << " "
             << j.CaiScore
             << endl;
    }
    for(auto & j : list_2){
        cout << j.StuId
             << " "
             << j.DeScore
             << " "
             << j.CaiScore
             << endl;
    }
    for(auto & j : list_3){
        cout << j.StuId
             << " "
             << j.DeScore
             << " "
             << j.CaiScore
             << endl;
    }
    for(auto & j : list_4){
        cout << j.StuId
             << " "
             << j.DeScore
             << " "
             << j.CaiScore
             << endl;
    }
}

bool cmp(StuInfo a,StuInfo b) //cmp函数
{
    if(a.sum != b.sum){
        return a.sum > b.sum;
    }
    else if(a.DeScore != b.DeScore){
        return a.DeScore > b.DeScore;
    }
    else{
        return a.StuId < b.StuId;
    }
}

 

 

四、总结

sort()函数发挥了很大的作用,具体用法:

#include <algorithm>
#include <functional>
#include <array>
#include <iostream>
#include <string_view>
 
int main()
{
    std::array<int, 10> s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
 
    auto print = [&s](std::string_view const rem) {
        for (auto a : s) {
            std::cout << a << ' ';
        }
        std::cout << ": " << rem << '\n';
    };
 
    std::sort(s.begin(), s.end());
    print("sorted with the default operator<");
 
    std::sort(s.begin(), s.end(), std::greater<int>());
    print("sorted with the standard library compare function object");
 
    struct {
        bool operator()(int a, int b) const { return a < b; }
    } customLess;
    std::sort(s.begin(), s.end(), customLess);
    print("sorted with a custom function object");
 
    std::sort(s.begin(), s.end(), [](int a, int b) {
        return a > b;
    });
    print("sorted with a lambda expression");
}
0 1 2 3 4 5 6 7 8 9 : sorted with the default operator<
9 8 7 6 5 4 3 2 1 0 : sorted with the standard library compare function object
0 1 2 3 4 5 6 7 8 9 : sorted with a custom function object
9 8 7 6 5 4 3 2 1 0 : sorted with a lambda expression

 

Guess you like

Origin blog.csdn.net/m0_50829573/article/details/121652691