zoukankan      html  css  js  c++  java
  • PAT 1015. 德才论 (25)

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

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

    输入格式:

    输入第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
    主要运用qsort排序。
    先把每种情况每类学生加入到不同数组中。在使用排序。
    注:用java写会运行超时。
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 #include<stdlib.h>
     5 typedef struct stu{
     6     int long sto;
     7     int de;
     8     int cai;
     9 }Stu;
    10 int cmp(const void*a,const void*b){
    11     Stu* c = (Stu*)a;
    12     Stu* d = (Stu*)b;
    13     int result = 0;
    14     result = -(c->de+c->cai-d->de-d->cai);
    15     if(result==0){
    16         result = -(c->de-d->de);
    17         if(result==0){
    18             result = c->sto-d->sto;
    19         }
    20     }
    21     return result;
    22 }
    23 Stu stu1[100010];
    24 Stu stu2[100010];
    25 Stu stu3[100010];
    26 Stu stu4[100010];
    27 int main(){
    28     int n,l,h;
    29     int st1=0,st2=0,st3=0,st4=0;
    30     scanf("%d %d %d",&n,&h,&l);
    31     for(int i=0;i<n;i++){
    32         int sto,de,cai;
    33         scanf("%d %d %d",&sto,&de,&cai);
    34         Stu stu;
    35         stu.sto = sto;
    36         stu.de = de;
    37         stu.cai = cai;
    38         if(de>=l&&cai>=l){
    39             stu1[st1++] = stu;
    40         }
    41         if(de>=l&&cai<l&&cai>=h){
    42             stu2[st2++] = stu;
    43         }
    44         if(de<l&&de>=h&&cai<l&&cai>=h&&de>=cai){
    45             stu3[st3++] = stu;
    46         }
    47         if(de<l&&de>=h&&cai>=h&&de<cai){
    48             stu4[st4++] = stu;
    49         }
    50                 
    51     }
    52     qsort(stu1,st1,sizeof(stu1[0]),cmp);
    53     qsort(stu2,st2,sizeof(stu2[0]),cmp);
    54     qsort(stu3,st3,sizeof(stu3[0]),cmp);
    55     qsort(stu4,st4,sizeof(stu4[0]),cmp);
    56     printf("%d
    ",st1+st2+st3+st4);
    57     for(int i=0;i<st1;i++){
    58         printf("%d %d %d
    ",stu1[i].sto,stu1[i].de,stu1[i].cai);
    59     }
    60     for(int i=0;i<st2;i++){
    61         printf("%d %d %d
    ",stu2[i].sto,stu2[i].de,stu2[i].cai);
    62     }
    63     for(int i=0;i<st3;i++){
    64         printf("%d %d %d
    ",stu3[i].sto,stu3[i].de,stu3[i].cai);
    65     }
    66     for(int i=0;i<st4;i++){
    67         printf("%d %d %d
    ",stu4[i].sto,stu4[i].de,stu4[i].cai);
    68     }
    69 }

  • 相关阅读:
    Office办公软件停止工作解决方案
    Jquery blockUI用法
    IE浏览器对js代码的高要求
    IIS中应用程序切换物理路径遇到的问题
    using关键字
    剑指offer-面试题23-链表中环的入口节点-双指针
    剑指offer-面试题22-链表中倒数第k个节点-双指针
    剑指offer-面试题21-调整数组顺序使奇数位于偶数前面-双指针
    剑指offer-面试题20-表示数值的字符串-字符串
    剑指offer-面试题19-正则表达式匹配-字符串
  • 原文地址:https://www.cnblogs.com/lolybj/p/6184143.html
Copyright © 2011-2022 走看看