码迷,mamicode.com
首页 > 其他好文 > 详细

[PAT乙级]德才论 (25)

时间:2018-04-11 21:43:15      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:mes   amp   相同   include   例子   class   long   UNC   space   

 

题目描述

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之

小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”



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

 

输入描述:

输入第1行给出3个正整数,分别为:N(<=105),即考生总数;L(>=60),为录取最低分数线,即德分和才分均不低于L的考生才有资格

被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到

但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于H,但是德分不低于才分的考生属于“才德兼

亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线L的考生也按总分排序,但排在第三类考生之后。


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



输出描述:

输出第1行首先给出达到最低分数线的考生人数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
#include<iostream>
#include<vector>
#include<string>
//#include<iomanip>
#include<algorithm>
//#include<functional>
#include<map>
#include<utility>
#include<numeric>
using namespace std;

bool cmp_by_value(const pair<long, vector<int>>& lhs, const pair<long, vector<int>>& rhs) {
    if (accumulate(lhs.second.begin(), lhs.second.end(), 0)
        == accumulate(rhs.second.begin(), rhs.second.end(), 0) && lhs.second[0] == rhs.second[0])
    {//德分与总分都并列
        return lhs.first < rhs.first;
    }
    if (accumulate(lhs.second.begin(), lhs.second.end(), 0)
        == accumulate(rhs.second.begin(), rhs.second.end(), 0))
    {//只有总分并列
        return lhs.second[0] > rhs.second[0];
    }

    return accumulate(lhs.second.begin(), lhs.second.end(), 0) 
            > accumulate(rhs.second.begin(), rhs.second.end(), 0);
}

int main()
{
    int N, L, H;
    cin >> N >> L >> H;
    long id;
    int df, cf;

    map<long, vector<int>> lv1, lv2, lv3, lv4;
    int count = 0;
    vector<pair<long, vector<int>>> vec1, vec2, vec3, vec4;

    while (N-- && cin >> id >> df >> cf)
    {
        if (df >= L && cf >= L)
        {
            ++count;
            if (df >= H && cf >= H)//德才兼备
            {
                vec1.push_back(make_pair(id, vector<int>{df, cf}));
            }
            else if (df >= H && cf < H)//才分不到地分到,但德胜于才
            {
                vec2.push_back(make_pair(id, vector<int>{df, cf}));
            }
            else if (df < H && cf < H && df > cf)//都不到H,但尚有德胜才
            {
                vec3.push_back(make_pair(id, vector<int>{df, cf}));
            }
            else
            {
                vec4.push_back(make_pair(id, vector<int>{df, cf}));
            }
        }
    }
    cout << count << endl;
    /*vector<pair<long, vector<int>>> vec1(lv1.begin(), lv1.end()), 
        vec2(lv2.begin(), lv2.end()), vec3(lv3.begin(), lv3.end()), vec4(lv4.begin(), lv4.end());*/
    sort(vec1.begin(), vec1.end(), cmp_by_value);
    for (auto n : vec1)
    {
        cout << n.first << " " << n.second[0] << " " << n.second[1] << endl;

    }
    sort(vec2.begin(), vec2.end(), cmp_by_value);
    for (auto n : vec2)
    {
        cout << n.first << " " << n.second[0] << " " << n.second[1] << endl;

    }
    sort(vec3.begin(), vec3.end(), cmp_by_value);
    for (auto n : vec3)
    {
        cout << n.first << " " << n.second[0] << " " << n.second[1] << endl;

    }
    sort(vec4.begin(), vec4.end(), cmp_by_value);
    for (auto n : vec4)
    {
        cout << n.first << " " << n.second[0] << " " << n.second[1] << endl;

    }

    system("pause");
    return 0;
}

以上是我第一次想的,不知道哪里出错了,希望看到的各位帮忙review一下~~

接下来是参考牛客某童鞋写的:

struct student {
    int no, a, b, sum, level;
    bool operator<(const student& l) const
    {
        if (level != l.level)//按等级排
            return level < l.level;
        else if (sum != l.sum)//按总分
            return sum > l.sum;
        else if (a != l.a)//按德分
            return a > l.a;
        else//按序号
            return no < l.no;
    }
};
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    int n, l, h, cnt = 0, no, a, b, sum;
    vector<student> vs;
    cin >> n >> l >> h;
    for (int i = 0; i != n; i++)
    {
        cin >> no >> a >> b;
        sum = a + b;
        if (a >= l && b >= l)
        {
            ++cnt;
            if (a >= h && b >= h)
            {
                vs.push_back({ no,a,b,sum,1 });
            }
            else if (a >= h)
            {
                vs.push_back({ no,a,b,sum,2 });
            }
            else if (a >= b)
            {
                vs.push_back({ no,a,b,sum,3 });
            }
            else {
                vs.push_back({ no,a,b,sum,4 });
            }
        }
    }
    sort(vs.begin(), vs.end());
    cout << cnt << endl;
    for (auto it = vs.begin(); it != vs.end(); it++)
        cout << it->no << " " << it->a << " " << it->b << endl;
    system("pause");
    return 0;
}

 

[PAT乙级]德才论 (25)

标签:mes   amp   相同   include   例子   class   long   UNC   space   

原文地址:https://www.cnblogs.com/jiadyang/p/8798536.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!