PAT乙級——1085(多標準排序)java實現
阿新 • • 發佈:2018-12-08
沒有AC,19分,暫時沒找到問題所在,又發現的請留言,我改正了也會更新
題目: PAT單位排行 (25 分)
每次 PAT 考試結束後,考試中心都會發佈一個考生單位排行榜。本題就請你實現這個功能。
輸入格式:
輸入第一行給出一個正整數 N(≤105),即考生人數。隨後 N 行,每行按下列格式給出一個考生的資訊:
准考證號 得分 學校
其中准考證號
是由 6 個字元組成的字串,其首字母表示考試的級別:B
代表乙級,A
代表甲級,T
代表頂級;得分
是 [0, 100] 區間內的整數;學校
是由不超過 6 個英文字母組成的單位碼(大小寫無關)。注意:題目保證每個考生的准考證號是不同的。
輸出格式:
首先在一行中輸出單位個數。隨後按以下格式非降序輸出單位的排行榜:
排名 學校 加權總分 考生人數
其中排名
是該單位的排名(從 1 開始);學校
是全部按小寫字母輸出的單位碼;加權總分
定義為乙級總分/1.5 + 甲級總分 + 頂級總分*1.5
的整數部分;考生人數
是該屬於單位的考生的總人數。
學校首先按加權總分排行。如有並列,則應對應相同的排名,並按考生人數升序輸出。如果仍然並列,則按單位碼的字典序輸出。
輸入樣例: 10 A57908 85 Au B57908 54 LanX A37487 60 au T28374 67 CMU T32486 24 hypu A66734 92 cmu B76378 71 AU A47780 45 lanx A72809 100 pku A03274 45 hypu 輸出樣例: 5 1 cmu 192 2 1 au 192 3 3 pku 100 1 4 hypu 81 2 4 lanx 81 2
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt (in.readLine());
String[] input = new String[num];
School[] school = new School[10000];
int count = 0;// 學校的個數
Set <School> set =new TreeSet<>();
for (int i = 0; i < 1000; i++)
school[i] = new School();
for (int i = 0; i < num; i++) {
input[i] = in.readLine();
}
in.close();
for (int i = 0; i < num; i++) {
Boolean schoolhas = false;
String[] value = input[i].split(" ");
char rate = value[0].charAt(0);// 考試等級
Loop: for (int j = 0; j < count; j++) {
if (value[2].equalsIgnoreCase(school[j].Name)) {
schoolhas = true;
switch (rate) {
case 'B':
school[j].score += Integer.parseInt(value[1]) * 2 / 3;
school[j].studentNum++;
break Loop;
case 'A':
school[j].score += Integer.parseInt(value[1]);
school[j].studentNum++;
break Loop;
default:
school[j].score += Integer.parseInt(value[1]) * 3 / 2;
school[j].studentNum++;
break Loop;
}
}
}
if (!schoolhas) {
school[count].Name = value[2].toLowerCase();
switch (rate) {
case 'B':
school[count].score += Integer.parseInt(value[1]) * 2 / 3;
school[count].studentNum++;
school[count].a = rate;
break;
case 'A':
school[count].score += Integer.parseInt(value[1]);
school[count].studentNum++;
school[count].a = rate;
break;
default:
school[count].score += Integer.parseInt(value[1]) * 3 / 2;
school[count].studentNum++;
school[count].a = rate;
break;
}
count++;
}
}
//加入TreeSet並排序
for (int i = 0; i < count; i++) {
set.add(school[i]);
}
int order =1;
int temp =0;
int score =0;
System.out.println(count);
for (School school2 :set) {
if(score==school2.score)
System.out.print(temp);
else {
System.out.print(order);
temp=order;//儲存上一位的排名
}
System.out.println(" " + school2.Name + " " + school2.score + " " + school2.studentNum);
score = school2.score;//儲存上一個人的分數
order++;//前面的人數
}
}
}
class School implements Comparable<School> {
String Name;
int score;
int studentNum;
char a;
@Override
public int compareTo(School o) {
//按照分數排序
int result = this.score - o.score;
if (result != 0)
return -result;
else {//按照學生個數排序
result = this.studentNum - o.studentNum;
if (result != 0)
return result;
else {//按字典碼排序,既每個字串依次按照ASCll表排序,小的在前,都相同就按照長度排
result = this.Name.compareTo(o.Name);
return result;
}
}
}
}
後面感覺可能是由於每一次都是取int型導致score不對,改用double型,且先相加再取整。
//效果一樣,還是19分,沒找到癥結所在
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(in.readLine());
String[] input = new String[num];
School[] school = new School[10000];
int count = 0;// 學校的個數
Set <School> set =new TreeSet<>();
for (int i = 0; i < 1000; i++)
school[i] = new School();
for (int i = 0; i < num; i++) {
input[i] = in.readLine();
}
in.close();
for (int i = 0; i < num; i++) {
Boolean schoolhas = false;
String[] value = input[i].split(" ");
char rate = value[0].charAt(0);// 考試等級
Loop: for (int j = 0; j < count; j++) {
if (value[2].equalsIgnoreCase(school[j].Name)) {
schoolhas = true;
switch (rate) {
case 'B':
school[j].bscore += Integer.parseInt(value[1]);
school[j].studentNum++;
break Loop;
case 'A':
school[j].ascore += Integer.parseInt(value[1]);
school[j].studentNum++;
break Loop;
default:
school[j].tscore += Integer.parseInt(value[1]);
school[j].studentNum++;
break Loop;
}
}
}
if (!schoolhas) {
//加入新的學校,名字小拼
school[count].Name = value[2].toLowerCase();
switch (rate) {
case 'B':
school[count].bscore += Integer.parseInt(value[1]);
school[count].studentNum++;
break;
case 'A':
school[count].ascore += Integer.parseInt(value[1]);
school[count].studentNum++;
break;
default:
school[count].tscore += Integer.parseInt(value[1]);
school[count].studentNum++;
break;
}
count++;
}
}
for (int i = 0; i < count; i++) {
school[i].score = (int)(school[i].bscore/1.5+school[i].ascore+school[i].tscore*1.5);
}
for (int i = 0; i < count; i++) {
set.add(school[i]);
}
//Arrays.sort(school,new );
int order =1;
int temp =0;
int score =0;
System.out.println(count);
for (School school2 :set) {
if(score==school2.score) //如果分數與前一個人相同,輸出前一個編號,temp不變
System.out.print(temp);
else {
System.out.print(order);
temp=order;//儲存上一位的排名
}
System.out.println(" " + school2.Name + " " + school2.score + " " + school2.studentNum);
score = school2.score;//儲存上一個人的分數
order++;//前面的人數
}
}
}
class School implements Comparable<School> {
String Name;
double bscore;
double ascore;
double tscore;
int score;
int studentNum;
@Override
public int compareTo(School o) {
int result = this.score - o.score;
if (result != 0)
return -result;
else {
result = this.studentNum - o.studentNum;
if (result != 0)
return result;
else {
result = this.Name.compareTo(o.Name);
return result;
}
}
}
}