• PAT乙级1015


    题目链接

    https://pintia.cn/problem-sets/994805260223102976/problems/994805307551629312

    题解

    思路比较简单,核心就是定义一个学生的排序规则:将考生分为4类(德和才分数都低于L的直接淘汰),先比较考生的类型,再比较分数或者准考证号,其中分数都是降序、准考证号是升序。

    淘汰直接在获取考生信息时进行;分类由Student构造函数实现;考生排序由stuCmp实现。

    // PAT BasicLevel 1015
    // https://pintia.cn/problem-sets/994805260223102976/problems/994805307551629312
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int N, L, H, M;
    class Student
    {
    public:
        string id;
        int de;
        int cai;
        int type;
        Student(string id,int de,int cai){
            this->id=id;this->de=de;this->cai=cai;
            if (de >= H && cai >= H){//才德全尽
                this->type=3;
            }else if(de>=H&&cai<H){//德胜才
                this->type = 2;
            }else if(de<H&&cai<H&&de>=cai){//“才德兼亡”但尚有“德胜才”者
                this->type = 1;
            }else{//达到最低线L
                this->type = 0;
            }
        }
        void print(){
            cout << id << ' ' << de << ' ' << cai << endl;
        }
    };
    
    
    bool stuCmp(Student&, Student&);
    
    int main()
    {
        // 考生数 最低录取线 优先录取线
        cin >> N >> L >> H;
        
        // 获取考生信息
        vector<Student> stuVec;
        string id;int de;int cai;
        for(int i=0;i<N;i++){
            cin >> id >> de >> cai;
            // 只存储cai和de不低于L的
            if (de >= L && cai >= L){
                stuVec.push_back(Student(id, de, cai));
                M++;
            }
        }
    
        // 学生排序
        sort(stuVec.begin(),stuVec.end(),stuCmp);
    
        // 输出结果
        cout << M << endl;
        for (vector<Student>::iterator it = stuVec.begin(); it != stuVec.end(); ++it){
            it->print();
        }
    
        //system("pause");
        return 0;
    }
    
    bool stuCmp(Student &s1, Student &s2)
    {
        if(s1.type==s2.type){// 同种type,比较总分
            if (s1.cai + s1.de == s2.cai + s2.de){
                if(s1.de==s2.de){
                    // id升序输出,其他都是降序输出的
                    return s1.id < s2.id;
                }else{
                    return s1.de>s2.de;
                }
            }else{
                return s1.cai + s1.de > s2.cai + s2.de;
            }
        }else{
            return s1.type>s2.type;
        }
    }
    

    作者:@臭咸鱼

    转载请注明出处:https://www.cnblogs.com/chouxianyu/

    欢迎讨论和交流!


  • 相关阅读:
    线程的中断.interrupt
    线程的加入.join()
    Runnable接口
    线程Thread类
    求和、均值
    最值、对应索引位置
    数组、冒泡排序
    获取Methods成员方法类
    获取Field成员变量类
    基于WinDbg的内存泄漏分析
  • 原文地址:https://www.cnblogs.com/chouxianyu/p/11303650.html
Copyright © 2020-2023  润新知