PAT 1015. 德才论 (25)

来源:互联网 发布:mac清理磁盘空间 编辑:程序博客网 时间:2024/05/01 21:59

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

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

输入格式:

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

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

输出格式:

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

输入样例:
14 60 8010000001 64 9010000002 90 6010000011 85 8010000003 85 8010000004 80 8510000005 82 7710000006 83 7610000007 90 7810000008 75 7910000009 59 9010000010 88 4510000012 80 10010000013 90 9910000014 66 60
输出样例:
1210000013 90 9910000012 80 10010000003 85 8010000011 85 8010000004 80 8510000007 90 7810000006 83 7610000005 82 7710000002 90 6010000014 66 6010000008 75 7910000001 64 90
题目分析:
(1)首先此题考查的是排序,本菜鸟一开始用冒泡法自然是运行超时的,所以快速排序值得考虑。
(2)另外,此题用printf明显才能所以通过,否则会出现运行超时。
(3)各种低于,不低于细心点,且第三类是德胜才,要小心一点。
#include <iostream>#include <string>#include <algorithm>#include <cstdio>using namespace std;struct score{    int number;    int de;    int cai;};int N;int L;int H;bool compare(score formal,score latter){    if ((formal.cai+formal.de)<(latter.cai+latter.de))        return false;    if ((formal.cai+formal.de)>(latter.cai+latter.de))        return true;    if (formal.de<latter.de)        return false;    if (formal.de>latter.de)        return true;    if (formal.number<latter.number)        return true;    else        return false;}int main (){    cin>>N;    cin>>L;    cin>>H;    score *income=new score [N];    for (int j=0; j<N; j++)    {        cin>>income[j].number;        cin>>income[j].de;        cin>>income[j].cai;    }    score outcome1[100000];    score outcome2[100000];    score outcome3[100000];    score outcome4[100000];    int a=0;    int b=0;    int c=0;    int d=0;    for (int i=0; i<N; i++)    {        if (income[i].de>=H&&income[i].cai>=H)        {            outcome1[a]=income[i];            a++;        }        else if (income[i].de>=H&&income[i].cai<H&&income[i].cai>=L)        {            outcome2[b]=income[i];            b++;        }        else if (income[i].de<H&&income[i].de>=L&&income[i].de>=income[i].cai&&income[i].cai>=L)        {            outcome3[c]=income[i];            c++;        }        else if (income[i].de>=L&&income[i].cai>=L)        {            outcome4[d]=income[i];            d++;        }    }    cout<<a+b+c+d<<endl;    sort(&outcome1[0],&outcome1[a],compare);    sort(&outcome2[0],&outcome2[b],compare);    sort(&outcome3[0],&outcome3[c],compare);    sort(&outcome4[0],&outcome4[d],compare);    for (int k1=0; k1<a; k1++)    {        //cout<<outcome1[k1].number<<' '<<outcome1[k1].de<<' '<<outcome1[k1].cai<<endl;        printf("%d %d %d\n",outcome1[k1].number,outcome1[k1].de,outcome1[k1].cai);    }    for (int k2=0; k2<b; k2++)    {       // cout<<outcome2[k2].number<<' '<<outcome2[k2].de<<' '<<outcome2[k2].cai<<endl;        printf("%d %d %d\n",outcome2[k2].number,outcome2[k2].de,outcome2[k2].cai);    }    for (int k3=0; k3<c; k3++)    {      //  cout<<outcome3[k3].number<<' '<<outcome3[k3].de<<' '<<outcome3[k3].cai<<endl;        printf("%d %d %d\n",outcome3[k3].number,outcome3[k3].de,outcome3[k3].cai);    }    for (int k4=0; k4<d; k4++)    {       // cout<<outcome4[k4].number<<' '<<outcome4[k4].de<<' '<<outcome4[k4].cai<<endl;        printf("%d %d %d\n",outcome4[k4].number,outcome4[k4].de,outcome4[k4].cai);    }    delete [] income;    return 0;}


                                             
0 0