PAT 德才論(乙級)

題目描述

宋代史學家司馬光在《資治通鑑》中有一段著名的“德才論”:“是故才德全盡謂之聖人,才德兼亡謂之愚人,德勝才謂之君子,才勝德謂之

小人。凡取人之術,苟不得聖人,君子而與之,與其得小人,不若得愚人。”



現給出一批考生的德才分數,請根據司馬光的理論給出錄取排名。

輸入描述:

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

代碼如下:

import java.util.*;
 public  class Main {
    public static void  main(String[] args) {
    	ArrayList <Student> al1=new ArrayList<Student>();
    	ArrayList <Student> al2=new ArrayList<Student>();
    	ArrayList <Student> al3=new ArrayList<Student>();
    	ArrayList <Student> al4=new ArrayList<Student>();
    	
    	Scanner in=new Scanner(System.in);
    	int n=Integer.parseInt(in.next());
    	int L=Integer.parseInt(in.next());
    	int H=Integer.parseInt(in.next());
    	Student stu[]=new Student[n];
    	int count=0;
    	for(int i=0;i<stu.length;i++){
    		stu[i]=new Student(in.next(),in.nextInt(),in.nextInt());
    		if(stu[i].morals>=H&&stu[i].talents>=H){
    			al1.add(stu[i]);
    		}
    		else if(stu[i].morals>=H&&stu[i].talents >=L){
    			al2.add(stu[i]);
    		}
    		else if(stu[i].morals>=L&&stu[i].talents>=L&&stu[i].morals>=stu[i].talents){
    			al3.add(stu[i]);
    		}
    		else if(stu[i].morals>=L&&stu[i].talents>=L)
    			al4.add(stu[i]);
    	}
    	System.out.println(al1.size()+al2.size()+al3.size()+al4.size());
    	   Comparator<Student> com = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.morals + o1.talents > o2.morals + o2.talents)
                    return -1;
                else if (o1.morals + o1.talents < o2.morals + o2.talents)
                    return 1;
                else {
                    if (o1.morals > o2.morals)
                        return -1;
                    else if (o1.morals < o2.morals)
                        return  1;
                    else {
                        if (Integer.valueOf(o1.id) < Integer.valueOf(o2.id))
                            return -1;
                        else
                            return 1;
                    }
                }
            }
        };
    	Collections.sort(al1,com);
    	Collections.sort(al2,com);
    	Collections.sort(al3,com);
    	Collections.sort(al4,com);
    	Iterator <Student> it=al1.iterator();
    	while(it.hasNext()){
    		Student st=it.next();
    		System.out.println(st.id+" "+st.morals+" "+st.talents);
    	}
    	Iterator <Student> it2=al2.iterator();
    	while(it2.hasNext()){
    		Student st=it2.next();
    		System.out.println(st.id+" "+st.morals+" "+st.talents);
    	}
    	Iterator <Student> it3=al3.iterator();
    	while(it3.hasNext()){
    		Student st=it3.next();
    		System.out.println(st.id+" "+st.morals+" "+st.talents);
    	}
    	Iterator <Student> it4=al4.iterator();
    	while(it4.hasNext()){
    		Student st=it4.next();
    		System.out.println(st.id+" "+st.morals+" "+st.talents);
    	}
    }
}

class Student{
	String id;
	int morals;
	int talents;
	public Student(String id,int morals,int talents){
		this.id=id;
		this.morals=morals;
		this.talents=talents;
	}
}

最近剛剛學到這裏,可是解題時還是出了問題,最後借鑑了大佬的答案。

不過還是有一些疑問,爲什麼我用第二個的時候會出現下面的錯誤?這兩個有什麼區別?


(一)

 Comparator<Student> com = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.morals + o1.talents > o2.morals + o2.talents)
                    return -1;
                else if (o1.morals + o1.talents < o2.morals + o2.talents)
                    return 1;
                else {
                    if (o1.morals > o2.morals)
                        return -1;
                    else if (o1.morals < o2.morals)
                        return  1;
                    else {
                        if (Integer.valueOf(o1.id) < Integer.valueOf(o2.id))
                            return -1;
                        else
                            return 1;
                    }
                }
            }
        };

(二)

class Mycompare implements Comparator<Student>{
	public int compare(Student s1,Student s2){
		if((s1.morals+s1.talents)>(s2.morals+s2.talents))
				return -1;
		else if((s1.morals+s1.talents)<(s2.morals+s2.talents))
			return 1;
		else {
			if(s1.morals>s2.morals)
				return -1;
			else if(s1.morals<s2.morals)
				return 1;
			else{
				if(Integer.valueOf(s1.id)>(Integer.valueOf(s2.id)))
					return -1;
				else 
					return 1;
			}
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章