前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >1015 德才论 (25 分)

1015 德才论 (25 分)

作者头像
可爱见见
发布2019-09-09 16:02:14
7810
发布2019-09-09 16:02:14
举报
文章被收录于专栏:卡尼慕卡尼慕

1015 德才论 (25 分)

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

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

输入格式:

输入第一行给出 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

思路

果然不愧是25分的题目,有点难度的,题目读懂了就会发现。我们仅需要按照题目的要求将学生分类即可,而分类这行为可以在输入的时候一并执行了。刚开始做的时候一直卡在排序问题!!这个排序是非常复杂的。。结果发现C++有自带的排序函数:sort(),还能自定义排序规则。。ummm,还是对库函数不熟悉。

先看一下这个排序算法,相当于积累吧。

sort()函数的排序:可以对数组元素和结构体数组排序; 对容器排序只能对vector, string, deque进行sort()

#include<iostream>
#include<algorithm>
using namespace std;

bool complare(int a,int b) {
    return a>b;
}
int main() {
    int a[10]={9,6,3,8,5,2,7,4,1,0};
    sort(a,a+10,complare); //在这里就不需要对complare函数传入参数了
    for(int i=0;i<10;i++)
        cout<<a[i]<<" ";
    return 0;
}

然后给出最终算法,因为使用到自定义的排序算法,就十分简单了。。

// 1015 德才论 (25 分).cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student
{
    //学号
    int num_student;
    //德分
    int de_score;
    //才分
    int cai_score;
};
//排序规则
bool cmp(Student a, Student b) {
    //总分不相等的情况下,按找总分降序排序。
    if (a.de_score + a.cai_score != b.de_score + b.cai_score)
        return a.de_score + a.cai_score > b.de_score + b.cai_score; 
    else{
        //总分相等的情况下,按照德分降序排序
        if (a.de_score != b.de_score)
            return a.de_score > b.de_score;
        else
            //德分与总分与才分均相等的情况下,按照考生考好升序排序
            return a.num_student < b.num_student;
    }
}
int main(){
    //输入
    int student_num;
    int score_zige;
    int score_youxian;
    cin >> student_num >> score_zige >> score_youxian;
    int i = student_num;
    //德分和才分均不低于此线的被定义为“才德全尽”
    vector<Student> de_cai_student;
    //才分不到但德分到线的一类考生属于“德胜才”
    vector<Student> de_vector;
    //德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者
    vector<Student> cai_vector;
    //剩下的及格的
    vector<Student> jige_vector;
    while (i) {
        int a, b, c;
        cin >> a >> b >> c;
        if (b >= score_youxian && c >= score_youxian) {
            Student student;
            student.de_score = b;
            student.cai_score = c;
            student.num_student = a;
            de_cai_student.push_back(student);
        }
        else if (b >= score_zige && c >= score_zige && b >= score_youxian && c < score_youxian) {
            Student student;
            student.de_score = b;
            student.cai_score = c;
            student.num_student = a;
            de_vector.push_back(student);
        }
        else if (b >= score_zige && c >= score_zige && b < score_youxian && c < score_youxian && b >= c) {
            Student student;
            student.de_score = b;
            student.cai_score = c;
            student.num_student = a;
            cai_vector.push_back(student);
        }
        else if (b >= score_zige && c >= score_zige) {
            Student student;
            student.de_score = b;
            student.cai_score = c;
            student.num_student = a;
            jige_vector.push_back(student);
        }
        i--;
    }
    //都是按照总分排序
    sort(de_cai_student.begin(), de_cai_student.end(), cmp);
    sort(de_vector.begin(), de_vector.end(), cmp);
    sort(cai_vector.begin(), cai_vector.end(), cmp);
    sort(jige_vector.begin(), jige_vector.end(), cmp);
    //输出
    cout << de_cai_student.size() + de_vector.size() + cai_vector.size() + jige_vector.size() << '\n';

    for (int i = 0; i < de_cai_student.size(); i++) {
        cout << de_cai_student[i].num_student << ' ' <<de_cai_student[i].de_score
            << ' ' << de_cai_student[i].cai_score << '\n';
    }
    for (int i = 0; i < de_vector.size(); i++) {
        cout << de_vector[i].num_student << ' ' << de_vector[i].de_score
            << ' ' << de_vector[i].cai_score << '\n';
    }
    for (int i = 0; i < cai_vector.size(); i++) {
        cout << cai_vector[i].num_student << ' ' << cai_vector[i].de_score
            << ' ' << cai_vector[i].cai_score << '\n';
    }
    for (int i = 0; i < jige_vector.size(); i++) {
        cout << jige_vector[i].num_student << ' ' << jige_vector[i].de_score
            << ' ' << jige_vector[i].cai_score;
        if (i != jige_vector.size()) {
            cout << '\n';
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-08-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 卡尼慕 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 输入格式:
  • 输出格式:
  • 输入样例:
  • 输出样例:
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档