Java-德才论 (25)

来源:互联网 发布:淘宝数据公式pv uv 编辑:程序博客网 时间:2024/05/17 08:59

题目描述

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入描述:

输入第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
import java.util.Scanner;import java.util.TreeSet;public class Person {//创建内部类用于存储考生信息,实现Comparable接口,用于排序private static class InnerPerson implements Comparable<InnerPerson>{private int id ;//考生准考证private int mScore;//考生德分private int aScore;//考生才分private int type;//考生类型(1才德全尽;2德胜才;3才德兼亡;4剩余)private int sumScore;//考生总分public InnerPerson(int id, int mscore, int ascore) {this.id = id;this.mScore = mscore;this.aScore = ascore;this.sumScore = this.mScore+this.aScore;//直接得出总分}public void printInfo(){System.out.println(this.id+" "+this.mScore+" "+this.aScore);}/* * 实现Comparable接口compareTo方法 * 1.首先判断考生类型,数值小的在前; * 2.对同类型的考生,比较其总分,总分高的在前; * 3.同类型且总分相同,则比较其德分,德分高的在前; * 4.上述三条都相同,比较其准考证,按照升序排列; */public int compareTo(InnerPerson p){if (this.type == p.type) {if (this.sumScore == p.sumScore) {if (this.mScore == p.mScore) {return ( this.id - p.id );}return (p.mScore - this.mScore);}return (p.sumScore- this.sumScore);}return (this.type - p.type);}}public static void main(String[] args) {Scanner in = new Scanner(System.in);int N = in.nextInt();//考生总数int minScore = in.nextInt();//录取及格分int priScore = in.nextInt();//优先录取分//创建TreeSet存储满足录取条件的考生,并在内部类定义时实现了排序方法TreeSet<InnerPerson> examineeList = new TreeSet<>();//读取每一行输入,将符合条件的加入到TreeSet中,并自动排序for(int i = 0; i < N; i++){int idtemp = in.nextInt();int mscore = in.nextInt();int ascore = in.nextInt();InnerPerson innerPerson = new InnerPerson(idtemp, mscore, ascore);innerPerson.id = idtemp;innerPerson.mScore = mscore;innerPerson.aScore = ascore;if (mscore >= minScore && ascore >= minScore) {if (mscore >= priScore && ascore >= priScore) {innerPerson.type = 1;examineeList.add(innerPerson);continue;}if (mscore >= priScore && ascore < priScore) {innerPerson.type = 2;examineeList.add(innerPerson);continue;}if (mscore >= ascore) {innerPerson.type = 3;}else{innerPerson.type = 4;}examineeList.add(innerPerson);}}System.out.println(examineeList.size());for (InnerPerson innerperson : examineeList) {innerperson.printInfo();}}}


原创粉丝点击