【PAT】(乙级)1015. 德才论 (25)

来源:互联网 发布:软件培训学校 北京 编辑:程序博客网 时间:2024/05/22 14:23

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 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

二、Tips

Tips1: 自定义数据类型排序、自定义cmp函数运算符重载vector
Tips2: 注意不低于(>=)和高于(>)的区别,临界条件要看清.

注: Java代码超时。(通过率:33.3%;超时:50%;格式错误:16.7%)。这个格式错误也是醉了。完全看不出来哪里不一样了。

三、代码

//注意不低于(>=)和高于(>)的区别,临界条件要看清#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <vector>using namespace std;int N,L,H;class stu{public:    int num;    int d;    int c;    stu(){        num=0;        d=0;        c=0;    }    stu(int _num,int _d,int _c){        num=_num;        d=_d;        c=_c;    }    int getType(){        if (d >= H && c >= H) {            return 1;        } else if (d >= H && c < H && c >= L) {            return 2;        } else if (d < H && d >= L && c < H && c >= L && d >= c) {            return 3;        } else {            return 4;        }    }    bool operator>(stu &s){        if(getType()!=s.getType()){            return getType()<s.getType();        }else{            if(c+d!=s.c+s.d){                return c+d>s.c+s.d;            }else{                if(d!=s.d){                    return d>s.d;                }else{                    return num<s.num;                }            }        }    }};bool cmp(stu s1,  stu s2){    return s1>s2;}int main(){    stu s1(101,80,80),s2(102,80,80);    int i,count=0;    scanf("%d %d %d",&N,&L,&H);    vector<stu> stus;    for(i=0;i<N;i++){        int num,d,c;        scanf("%d %d %d",&num,&d,&c);        if(d<L||c<L){            continue;        }        stu s(num,d,c);        count++;        stus.push_back(s);    }    sort(stus.begin(),stus.end(), cmp);    cout<<count<<endl;    for(int i=0;i<count;i++){        printf("%d %d %d\n",stus[i].num,stus[i].d,stus[i].c);    }    return 0;}

注:下方Java代码未AC。仅做学习之用。

import java.util.*;/** * 1015. 德才论 (25) * https://www.patest.cn/contests/pat-b-practise/1015 超时 通过率:33% * Created by Relish on 2016/8/7. */public class _1015 {    private static int N, L, H;    public static void main(String[] args) {        Scanner cin = new Scanner(System.in);        N = cin.nextInt();//num of students        L = cin.nextInt();//lowest score        H = cin.nextInt();//priority line        List<Student> students = new ArrayList<Student>();        for (int i = 0; i < N; i++) {            Student student = new Student(cin.nextInt(), cin.nextInt(), cin.nextInt());            if (student.beConsidered()) {                students.add(student);            }        }        Collections.sort(students);        System.out.println(students.size());        System.out.println(Arrays.toString(students.toArray()).replaceAll("\\[|\\]", "").replace(", ", "\n"));    }    static class Student implements Comparable<Student> {        int num;        int d;        int c;        Student(int num, int d, int c) {            this.num = num;            this.d = d;            this.c = c;        }        @Override        public int compareTo(Student o) {            if (getType() < o.getType()) {                return -1;            } else if (getType() == o.getType()) {                if (c + d > o.c + o.d) {                    return -1;                } else if (c + d == o.c + o.d) {                    if (d > o.d) {                        return -1;                    } else if (d == o.d) {                        return num > o.num ? 1 : -1;//never equals                    } else {                        return 1;                    }                } else {                    return 1;                }            } else {                return 1;            }        }        int getType() {            if (d >= H && c >= H) {                return 1;            } else if (d >= H && c < H && c >= L) {                return 2;            } else if (d < H && d >= L && c < H && c >= L && d >= c) {                return 3;            } else {                return 4;            }        }        boolean beConsidered() {            return c >= L && d >= L;        }        @Override        public String toString() {            return num + " " + d + " " + c;        }    }}
0 0
原创粉丝点击