PAT乙级 1015. 德才论

来源:互联网 发布:源码安装php7.1 编辑:程序博客网 时间:2024/05/02 02:29

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

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

输入格式:

输入第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 79

10000001 64 90

:此题用python提交是肯定过不了的,时间不够,所以用C++,超时的python代码同样贴出来,供参考。

代码如下(C++):

#include <stdio.h>#include <iostream>#include <vector>#include <algorithm>using namespace std;struct student{    int num;    int d;    int c;    bool operator <(const student &A) const{        if(d+c != A.d+A.c)            return d+c>A.d+A.c;        else if(d != A.d)            return d>A.d;        else return num<A.num;    }};int main(){    int n,l,h,i,count=0;    vector<student> v1,v2,v3,v4;    student temp;    scanf("%d %d %d",&n,&l,&h);    for(i=0;i<n;i++)    {        scanf("%d %d %d",&temp.num,&temp.d,&temp.c);        if((temp.d>=l)&&(temp.c>=l))        {            count++;            if((temp.d>=h)&&(temp.c>=h))                v1.push_back(temp);            else if(temp.d>=h)                v2.push_back(temp);            else if(temp.d>=temp.c)                v3.push_back(temp);            else v4.push_back(temp);        }    }    sort(v1.begin(),v1.end());    sort(v2.begin(),v2.end());    sort(v3.begin(),v3.end());    sort(v4.begin(),v4.end());    vector<student>::iterator itr;    printf("%d\n",count);    for(itr=v1.begin();itr!=v1.end();++itr)        printf("%d %d %d\n",itr->num,itr->d,itr->c);    for(itr=v2.begin();itr!=v2.end();++itr)        printf("%d %d %d\n",itr->num,itr->d,itr->c);    for(itr=v3.begin();itr!=v3.end();++itr)        printf("%d %d %d\n",itr->num,itr->d,itr->c);    for(itr=v4.begin();itr!=v4.end();++itr)        printf("%d %d %d\n",itr->num,itr->d,itr->c);    system("pause");    return 0;}

(python):

__author__ = 'lywade'n, l, h = map(int,raw_input().split())input_list = [map(int,raw_input().split()) for i in range(n)]#print input_listfirst_list, second_list,third_list, fourth_list = [],[],[],[]for i in range(n):    if input_list[i][1] >= h and input_list[i][2] >= h:        first_list.append(input_list[i])    elif input_list[i][1] >= h and l <= input_list[i][2] <= h:        second_list.append(input_list[i])    elif l <= input_list[i][1] <= h and l <= input_list[i][2] <= h and input_list[i][1] >= input_list[i][2]:        third_list.append(input_list[i])    elif input_list[i][1] >= l and input_list[i][2] >= l:        fourth_list.append(input_list[i])#print first_list,second_list,third_list,fourth_listdef sort_list(list):    def mycmp(a, b):        if a[1]+a[2] < b[1]+b[2]:            return 1        elif a[1]+a[2] > b[1]+b[2]:            return -1        else:            if a[1] < b [1]:                return 1            elif a[1] > b[1]:                return -1            else:                if a[0] > b[0]:                    return 1                elif a[0] < b[0]:                    return -1    list.sort(cmp=mycmp)    return listdef print_list(list):    for i in list:        print ' '.join(map(str,i))print len(first_list)+len(second_list)+len(third_list)+len(fourth_list)print_list(sort_list(first_list))print_list(sort_list(second_list))print_list(sort_list(third_list))print_list(sort_list(fourth_list))

0 0
原创粉丝点击