PAT (Basic Level) Practise (中文)1015. 德才论(3种方法)

来源:互联网 发布:剑与家园 知乎 编辑:程序博客网 时间:2024/06/01 09:50

1015. 德才论 (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Li

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

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

输入格式:

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


题目难点在于对结构体类型的排序,下方代码也是看了前辈(@脱水的兔子)的博客和qsort()的用法后才最终AC的。之后我又测试了自己之前的方法,没有用malloc,而是直接开了4个student类型的100005大小的数组来存储不同等级的考生成绩,只要写对了cmp()函数就可以AC,未出现内存溢出的问题,但是这种方式有2个要注意的地方:①定义很大数组要写在main()函数之外;②同样需要用指针来写qsort()的排序函数(写好此函数才是AC的关键)。


【高能来了】在看博客的过程中,发现用C++STL里的vector容器来做很舒服,可以避开繁琐、易出bug的指针,学习之后也用vector做了一遍,成功AC。


所以这个题算是有3种方法:vector、数组、指针,其中前两种大同小异(指针和数组是好基友你懂得)。


资料参考


malloc版题解博客:http://blog.csdn.net/wyxdexyq/article/details/40345867?locationNum=1&fps=1

《c/c++中关于qsort的使用》:http://www.cnblogs.com/CCBB/archive/2010/01/15/1648827.html

《c++ vector的用法》:http://www.cnblogs.com/wang7/archive/2012/04/27/2474138.html

vector版题解博客:http://blog.csdn.net/xtzmm1215/article/details/38443103


AC代码1:

#include <stdio.h>#include <stdlib.h>#include <malloc.h>struct student{    int num,d,c;//学号,德分,才分};//qosrt方法的排序函数(指针)int cmp( const void *p,const void *q ){    if( (*(student*)p).d + (*(student*)p).c != (*(student*)q).d + (*(student*)q).c )//德才总分不等,则按总分的降序排列        return (*(student*)q).d + (*(student*)q).c - (*(student*)p).d - (*(student*)p).c;    else{//总分相等        if( (*(student*)p).d != (*(student*)q).d ){//按德分降序排列            return (*(student*)q).d - (*(student*)p).d;        }        else{//德分也相等            return (*(student*)p).num - (*(student*)q).num;//按考号升序排列        }    }}int main(){    int n,l,h;    scanf("%d%d%d",&n,&l,&h);//考生数,低分线,高分线    //为指针申请空间(相当于建立数组)    student *p1 = (student*)malloc(100000*sizeof(student));    student *p2 = (student*)malloc(100000*sizeof(student));    student *p3 = (student*)malloc(100000*sizeof(student));    student *p4 = (student*)malloc(100000*sizeof(student));    int l1=0,l2=0,l3=0,l4=0;    int n_pass=n;//被录取人数    student temp;    for( int i=0;i<n;++i ){        scanf("%d%d%d",&temp.num,&temp.d,&temp.c);        if( temp.d<l || temp.c<l ){            --n_pass;        }        else if( temp.d>=h && temp.c>=h ){//德才兼备            p1[l1++]=temp;        }        else if( temp.d>=h && temp.c<h ){//德胜才            p2[l2++]=temp;        }        else if( temp.d<h && temp.c<h && temp.d>=temp.c ){//才德兼亡但德胜才            p3[l3++]=temp;        }        else{            p4[l4++]=temp;//其他        }    }    //qsort(数组首地址,元素数量,元素占内存字节数,自定义的排序函数)    qsort( p1,l1,sizeof(student),cmp );    qsort( p2,l2,sizeof(student),cmp );    qsort( p3,l3,sizeof(student),cmp );    qsort( p4,l4,sizeof(student),cmp );    printf("%d\n",n_pass);    for( int i=0;i<l1;++i ){        printf("%d %d %d\n",p1[i].num,p1[i].d,p1[i].c);    }    for( int i=0;i<l2;++i ){        printf("%d %d %d\n",p2[i].num,p2[i].d,p2[i].c);    }    for( int i=0;i<l3;++i ){        printf("%d %d %d\n",p3[i].num,p3[i].d,p3[i].c);    }    for( int i=0;i<l4;++i ){        printf("%d %d %d\n",p4[i].num,p4[i].d,p4[i].c);    }    return 0;}

AC代码2:

#include <iostream>#include <stdio.h>#include <vector>#include <algorithm>using namespace std;struct student{    int num;    int d;    int c;    bool operator < (const student &x) const{//在结构体内部定义比较函数(即定义什么是'<')        if( d+c != x.d+x.c ){            return d+c>x.d+x.c;//德才总分高的排在前面        }        else{            if( d != x.d ){                return d>x.d;//总分相同,德分高的排在前面            }            else{                return num<x.num;//德分也相同,考号小的排在前面            }        }    }};int main(){    int n,l,h;    scanf("%d%d%d",&n,&l,&h);    vector<student> p1,p2,p3,p4;//定义4个vector来存储    student temp;    int n_pass=n;    for( int i=0;i<n;++i ){        scanf("%d%d%d",&temp.num,&temp.d,&temp.c);        if( temp.d<l || temp.c<l ){            --n_pass;        }        else if( temp.d>=h && temp.c>=h ){            p1.push_back(temp);//在p1容器后方压入temp        }        else if( temp.d>=h && temp.c<h ){            p2.push_back(temp);        }        else if( temp.d<h && temp.c<h && temp.d>=temp.c ){            p3.push_back(temp);        }        else{            p4.push_back(temp);        }    }    //sort()方法对vector内数据的排序(结构体内已定义好排序函数)    sort( p1.begin(),p1.end() );    sort( p2.begin(),p2.end() );    sort( p3.begin(),p3.end() );    sort( p4.begin(),p4.end() );    printf("%d\n",n_pass);    vector<student>::iterator itr;//定义一个vector专用的迭代器(类似于平时写的for循环里的i,j)    for( itr=p1.begin();itr!=p1.end();++itr ){//循环条件可换成itr<p1.end();(end()指向verctor最后一个元素的下一个位置)        printf("%d %d %d\n",itr->num,itr->d,itr->c);    }    for( itr=p2.begin();itr!=p2.end();++itr ){        printf("%d %d %d\n",itr->num,itr->d,itr->c);    }    for( itr=p3.begin();itr!=p3.end();++itr ){        printf("%d %d %d\n",itr->num,itr->d,itr->c);    }    for( itr=p4.begin();itr!=p4.end();++itr ){        printf("%d %d %d\n",itr->num,itr->d,itr->c);    }    return 0;}


1 0