B1015. 德才论 (25)

来源:互联网 发布:虚拟机上ubuntu服务器 编辑:程序博客网 时间:2024/06/08 13:58

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

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

输入格式:

输入第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 <cstdio>  #include <cstdlib> #include<vector>#include <algorithm>using namespace std;struct Student{    int ID = 0;    int moral = 0;    int talent = 0;    int sum = 0;    int type = 0;}student;bool cmp(Student a,Student b){    if (a.type != b.type)        return a.type < b.type;    else if (a.sum != b.sum)        return a.sum > b.sum;    else if (a.moral != b.moral)        return a.moral > b.moral;    else        return a.ID < b.ID;}int main(){    int num = 0, low = 0, high = 0, valid = 0;    vector<Student> list;    cin >> num >> low >> high;    //录入满足L线的学生    for (int i = 0; i < num; ++i)    {        //cin >> student.ID >> student.moral >> student.talent;        scanf_s("%ld%d%d", &student.ID, &student.moral, &student.talent);        student.sum = student.moral + student.talent;        if (student.moral < low || student.talent < low)//淘汰            student.type = 4;        else if (student.moral >= high&& student.talent >= high)//第一类            student.type = 0;        else if (student.moral >= high&&student.talent < high)//第二类            student.type = 1;        else if (student.moral >= student.talent)//第三类            student.type = 2;        else//第四类            student.type = 3;        if (student.type == 4)            continue;        list.push_back(student);        ++valid;    }    sort(list.begin(), list.end(),cmp);    cout << valid;    for (vector<Student>::iterator iter = list.begin(); iter != list.end(); ++iter)        printf("\n%ld %d %d", iter->ID, iter->moral, iter->talent);        //cout << endl << iter->ID<<' ' << iter->moral << ' ' << iter->talent;    system("pause");    return 0;}

用的冒泡算法,超时,改为sort;
cmp不够精简,超时,继续改,还是超时;
用的cin,cout输入输出,超时,改为scanf_s和printf;(某前辈的博客提到过,还好我有印象)
使用scanf_s,PAT提示编译错误,提交代码时改为scanf;
终于结束了,提交了10多次。。。。。学渣真是参考别人的代码都很吃力;
代码参考了前辈的,在此感谢:http://blog.csdn.net/ysc6688/article/details/39251425
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
学到了:
1、c比c++运行的快;
2、sort的使用;
3、vector的使用;

0 0