【PAT 乙級(Basic Level)】德才論
阿新 • • 發佈:2019-02-09
呃,這次好尷尬,我發現自己對Java還不是一般的不瞭解。一個題目,暴露了好多問題。出題者的意圖基本沒get到,連內部類都是排序的時候才發現的。
一、難點
排序要求是先按總分從高到低排,如果總分相同則對比德分,按德分從高到低往下排,若德分也相同,則按准考證號大小從小到大往下排。
二、解決方案
失敗的解決方案:我想用快速排序實現,但是發現用總分排序完以後沒辦法再往下細分了。簡單地說就是,我掌握的快速排序只能按一個標準排序(我現在懷疑我到底有沒有掌握快速排序)。昨天還覺得自己思路清晰,今天瞬間發現自己就是個蠢貨。
三、最終答案
import java.util.Collections; import java.util.Comparator; import java.util.Scanner; import java.util.Vector; public class Main { private class Student { private String id;// 准考證 private int virtue;// 德分 private int ability;// 才分 public String getId() { return id; } public void setId(String id) { this.id = id; } public int getVirtue() { return virtue; } public void setVirtue(int virtue) { this.virtue = virtue; } public int getAbility() { return ability; } public void setAbility(int ability) { this.ability = ability; } public int getSum(){ return this.getVirtue()+this.getAbility(); } } private class MyComparator1 implements Comparator { public int compare(Object o1, Object o2) { Student stu1 = (Student) o1; Student stu2 = (Student) o2; if (stu1.getSum() != stu2.getSum()) { return stu2.getSum() - stu1.getSum(); } else if (stu1.getVirtue()!= stu2.getVirtue()) { return stu2.getVirtue()- stu1.getVirtue(); } else return stu1.getId().compareTo(stu2.getId()); } } public static void main(String[] args) { Scanner s = new Scanner(System.in); int m = s.nextInt();// 考生人數 int L = s.nextInt();// 錄取線 int H = s.nextInt();// 優先錄取線 Vector inV = new Vector();// 錄取佇列 Vector firstV = new Vector();// 德才兼備,德>=H,才>=H Vector secondeV = new Vector();// 德勝才,德>=H,才<H Vector thirdV = new Vector();// 才德兼亡但德勝才,德>=才,德<H,才<H Vector fourV = new Vector();// 德<H,才<H,德<=才 for (int i = 0; i < m; i++) { Main.Student student = new Main().new Student(); student.setId(s.next()); student.setVirtue(s.nextInt()); student.setAbility(s.nextInt()); if (student.getVirtue() >= L && student.getAbility() >= L) { inV.addElement(student); } } for (int i = 0; i < inV.size(); i++) { Main.Student temp = new Main().new Student(); temp = (Student) inV.elementAt(i); if (temp.getVirtue() >= H && temp.getAbility() >= H) { firstV.addElement(temp); }else if (temp.getVirtue() >= H && temp.getAbility() < H) { secondeV.addElement(temp); }else if (temp.getVirtue() < H && temp.getAbility() < H && (temp.getVirtue() >= temp.getAbility())) { thirdV.addElement(temp); } else{ fourV.addElement(temp); } } System.out.println(inV.size()); Comparator cm = new Main(). new MyComparator1(); Collections.sort(firstV, cm); Collections.sort(secondeV, cm); Collections.sort(thirdV, cm); Collections.sort(fourV, cm); for (int i = 0; i < firstV.size(); i++) { Student student = (Student) firstV.elementAt(i); System.out.println(student.getId()+" "+student.getVirtue()+" "+student.getAbility()); } for (int i = 0; i < secondeV.size(); i++) { Student student = (Student) secondeV.elementAt(i); System.out.println(student.getId()+" "+student.getVirtue()+" "+student.getAbility()); } for (int i = 0; i < thirdV.size(); i++) { Student student = (Student) thirdV.elementAt(i); System.out.println(student.getId()+" "+student.getVirtue()+" "+student.getAbility()); } for (int i = 0; i < fourV.size(); i++) { Student student = (Student) fourV.elementAt(i); System.out.println(student.getId()+" "+student.getVirtue()+" "+student.getAbility()); } } }
四、總結
這個題目算是把我徹底暴露了。遇到如下問題:
2、儲存物件用Vector代替陣列,按我的理解,其實就是一個儲存空間的問題,一個長度是固定的,一個是可擴充的。好多人都用Vector,我相信群眾的眼睛是雪亮的(其實是覺得自己太淺薄狹隘了)。
3、比較器Comparator的運用。Comparator可以進行復雜的排序,尤其是本題這種情況,完美適配。我剛剛才知道這個強大的介面。http://muscle-liu.iteye.com/blog/157261
五、無用的程式碼
我自己寫的排序,捨不得刪了,留念而已:
private void quickSort(int[] sort, int left, int right) { int dp; if (left < right) { //第一次迴圈,通常不能保證排序正確,但是能保證左邊的數全部小於temp,右邊的數全部大於temp dp = partition(sort, left, right); System.out.println("partition(sort,"+left+", "+right+")結果為:"); for (int i = 0; i < 5; i++) { System.out.print("sort[" + i + "]=" + sort[i]+" "); } System.out.println(); //由於第一次迴圈結束已經將序列分成了兩部分,現在排序左邊的部分 quickSort(sort, left, dp - 1); System.out.println("quickSort(sort,"+ left+", "+(dp - 1)+")結果為(此時dp="+dp+"):"); for (int i = 0; i < 5; i++) { System.out.print("sort[" + i + "]=" + sort[i]+" "); } System.out.println(); //排序右邊的部分 quickSort(sort, dp + 1, right); System.out.println("quickSort(sort, "+(dp + 1)+", "+right+")結果為(此時dp="+dp+"):"); for (int i = 0; i < 5; i++) { System.out.print("sort[" + i + "]=" + sort[i]+" "); } System.out.println(); } } public static void main(String[] args) { int sort[] = { 20,40,50,10,60}; System.out.println("原始資料:"); for (int i = 0; i < 5; i++) { System.out.print("sort[" + i + "]=" + sort[i]+" "); } Quick q=new Quick(); q.quickSort(sort, 0, 4); }
搜尋
複製