生活随笔
收集整理的這篇文章主要介紹了
pat德才论(java)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
刷pat時候發現德才論這個題目真有點煩。用到的知識點比較重要。題目鏈接
題意:
鏈接:https://www.nowcoder.com/questionTerminal/97b6a49a85944650b2e3d0660b91c324
來源:牛客網
/**
- 德才論
- 題目描述
- 宋代史學家司馬光在《資治通鑒》中有一段著名的“德才論”:“是故才德全盡謂之圣人,才德兼亡謂之愚人,
- 德勝才謂之君子,才勝德謂之小人。凡取人之術,茍不得圣人,君子而與之,與其得小人,不若得愚人。”
- 現給出一批考生的德才分數,請根據司馬光的理論給出錄取排名。
- 輸入描述:
- 輸入第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
*/
總體思路:建立一個類包含學號,得分,和才分。用四種集合分別存入輸入的符合題意的。然后對集合進行自定義快排。
但是我不是這么想的,我想利用優先隊列讓他自動排序。我只需要設置他是大根堆還是小根堆就行了。代碼如下:
package 乙
;import java
.io
.BufferedReader
;
import java
.io
.IOException
;
import java
.io
.InputStreamReader
;
import java
.io
.OutputStreamWriter
;
import java
.io
.PrintWriter
;
import java
.io
.StreamTokenizer
;
import java
.util
.ArrayList
;
import java
.util
.Comparator
;
import java
.util
.List
;
import java
.util
.PriorityQueue
;
import java
.util
.Queue
;
import java
.util
.Scanner
;public class pat5 {public static void main(String
[] args
) throws IOException
{StreamTokenizer in
=new StreamTokenizer(new BufferedReader(new InputStreamReader(System
.in
)));PrintWriter out
= new PrintWriter(new OutputStreamWriter(System
.out
));in
.nextToken();int allcount
=(int)in
.nval
;int count
=0;in
.nextToken();int last
=(int)in
.nval
;in
.nextToken();int well
=(int)in
.nval
;student stu
[]=new student[allcount
];Queue
<student> c1
= new PriorityQueue<>(cmp1
);Queue
<student> c2
= new PriorityQueue<>(cmp1
);Queue
<student> c3
= new PriorityQueue<>(cmp1
);Queue
<student> c4
= new PriorityQueue<>(cmp1
);for(int i
=0;i
<allcount
;i
++){in
.nextToken(); int no1
=(int)in
.nval
;in
.nextToken(); int no2
=(int)in
.nval
;in
.nextToken(); int no3
=(int)in
.nval
;stu
[i
]=new student(no1
,no2
,no3
);}for(int i
=0;i
<allcount
;i
++){if(stu
[i
].de
>=last
&&stu
[i
].cai
>=last
){count
++;if(stu
[i
].cai
>=well
&&stu
[i
].de
>=well
){c1
.add(stu
[i
]);}else if(stu
[i
].cai
>=last
&&stu
[i
].de
>=well
){c2
.add(stu
[i
]);}else if(stu
[i
].de
>=stu
[i
].cai
&&stu
[i
].cai
<well
){c3
.add(stu
[i
]);}else{c4
.add(stu
[i
]);}}}out
.println(count
);while(!c1
.isEmpty()){student st
=c1
.poll();out
.println(st
.number
+" "+st
.de
+" "+st
.cai
);}while(!c2
.isEmpty()){student st
=c2
.poll();out
.println(st
.number
+" "+st
.de
+" "+st
.cai
);}while(!c3
.isEmpty()){student st
=c3
.poll();out
.println(st
.number
+" "+st
.de
+" "+st
.cai
);}while(!c4
.isEmpty()){student st
=c4
.poll();out
.println(st
.number
+" "+st
.de
+" "+st
.cai
);}out
.flush();}static Comparator
<student>cmp1
=new Comparator<student>(){ public int compare(student a
, student b
) {if(a
.de
+a
.cai
==b
.cai
+b
.de
){if(a
.de
==b
.de
){return a
.number
-b
.number
;}else{return b
.de
-a
.de
;}}elsereturn (int)(b
.cai
+b
.de
-a
.de
-a
.cai
);} };static class student{int number
;int de
;int cai
;public student(int number
,int de
,int cai
){this.number
=number
;this.de
=de
;this.cai
=cai
;}}
}
水平有限。。有好想法的多多交流。
總結
以上是生活随笔為你收集整理的pat德才论(java)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。