考試排名
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16041 Accepted Submission(s): 5604
Problem Description C++程式設計考試使用的實時送出系統,具有即時獲得成績排名的特點。它的功能是怎麼實作的呢?
我們做好了題目的解答,送出之後,要麼“AC”,要麼錯誤,不管怎樣錯法,總是給你記上一筆,表明你曾經有過一次錯誤送出,因而當你一旦送出該題“AC”後,就要與你算一算帳了,總共該題錯誤送出了幾回。雖然你在題數上,大步地躍上了一個台階,但是在耗時上要攤上你共花去的時間。特别是,曾經有過的錯誤送出,每次都要攤上一定的機關時間分。這樣一來,你在做出的題數上,可能領先别人很多,但是,在做出同樣題數的人群中,你可能會在耗時上處于排名的劣勢。
例如:某次考試一共8題(A,B,C,D,E,F,G,H),每個人做的題都在對應的題号下有個數量标記,負數表示該學生在該題上有過的錯誤送出次數,但到現在還沒有AC,正數表示AC所耗的時間,如果正數a跟上一對括号,裡面有個整數b,那就表示該學生送出該題AC了,耗去了時間a,同時,曾經錯誤送出了b次,是以對于下述輸入資料:
若每次錯誤送出的罰分為20分,則其排名從高到低應該是這樣的:
Josephus 5 376
John 4 284
Alice 4 352
Smith 3 167
Bob 2 325
Bush 0 0
Input 輸入資料的第一行是考試題數n(1≤n≤12)以及機關罰分數m(10≤m≤20),每行資料描述一個學生的使用者名(不多于10個字元的字串)以及對所有n道題的答題現狀,其描述采用問題描述中的數量标記的格式,見上面的表格,送出次數總是小于100,AC所耗時間總是小于1000。
Output 将這些學生的考試現狀,輸出一個實時排名。實時排名顯然先按AC題數的多少排,多的在前,再按時間分的多少排,少的在前,如果湊巧前兩者都相等,則按名字的字典序排,小的在前。每個學生占一行,輸出名字(10個字元寬),做出的題數(2個字元寬,右對齊)和時間分(4個字元寬,右對齊)。名字、題數和時間分互相之間有一個空格。
Sample Input
8 20
Smith -1 -16 8 0 0 120 39 0
John 116 -2 11 0 0 82 55(1) 0
Josephus 72(3) 126 10 -3 0 47 21(2) -2
Bush 0 -1 -8 0 0 0 0 0
Alice -2 67(2) 13 -1 0 133 79(1) -1
Bob 0 0 57(5) 0 0 168 -7 0
Sample Output
Josephus 5 376
John 4 284
Alice 4 352
Smith 3 167
Bob 2 325
Bush 0 0
其實這個題目就是考察程式設計能力的,我的代碼結果能出來,就是一直PE(Presentation Error)解決不了 --2017-11-20--
現在已經解決了,問題是這樣的:
System.out.printf("%-10s %2d %4d\n", person.getName(), person.getAcNum(), person.getScore());
當我們把\n放到printf裡面的時候,在eclipse/myeclipse是可以正确列印的 但是在HDOJ裡面應該是不算數的,需要另外寫一個換行的句子:
System.out.printf("%-10s %2d %4d", person.getName(), person.getAcNum(), person.getScore());
System.out.println();
這樣就不會PE了
題目比較長,看起來可能會有點吃力,大概是這樣的: 輸入的第一行:第一個是AC的題目總數,第二個是機關罰分 下面的就是:第一個是名字,之後的是AC題目個數或者用時情況 具體是: 負數表示:該學生在該題上有過的錯誤送出次數,但到現在還沒有AC 整數表示:正數表示AC所耗的時間 括号裡的數:曾經錯誤送出了b次
那麼怎麼來排名的呢? 首先是按照AC的題目總數,也就是正數的個數 然後是按照罰分總分來排,小的在前(正數加上括号裡的數乘以機關罰分) 最後是名字
至于列印,題目沒直接的提示,其實是放到while()外面的
View Code
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
private static Scanner scanner;
static List<Main.person> list;
public static void main(String[] args) {
scanner = new Scanner(System.in);
int probrom = scanner.nextInt();// 題目數量
int pubScore = scanner.nextInt();// 機關罰分
list = new ArrayList<Main.person>();
while (scanner.hasNext()) {
int ac = 0;// ac題目個數
int score = 0;// 罰分
String name = "";// 姓名
String strings[] = new String[probrom + 1];
// 輸入一個考生的資訊
// strings[0]是名字
for (int i = 0; i < strings.length; i++) {
strings[i] = scanner.next();
}
name = strings[0];
// 計算AC題目個數
for (int i = 1; i < strings.length; i++) {
char ch = strings[i].charAt(0);
if (ch >= '1' && ch <= '9') {
ac++;
try {
score += Integer.parseInt(strings[i]);
} catch (Exception e) {
}
}
}
// 計算分數
for (int i = 1; i < strings.length; i++) {
// 第i個元素的 最後一個字元是否為')'// 也可以使用contains
char ch = strings[i].charAt(strings[i].length() - 1);
if (ch == ')') {
String[] split = strings[i].split("[(,)]");
score += pubScore * Integer.parseInt(split[1]) + Integer.parseInt(split[0]);
}
}
Main p = new Main();
person per = p.new person(ac, score, name);
list.add(per);
}
listSort();
for (person person : list) {
System.out.printf("%-10s %2d %4d", person.getName(), person.getAcNum(), person.getScore());
System.out.println();
}
}
// 排序
private static void listSort() {
for (int i = 0; i < list.size() - 1; i++) {
for (int j = i + 1; j < list.size(); j++) {
String iName = list.get(i).getName();
String jName = list.get(j).getName();
int iAc = list.get(i).getAcNum();
int jAc = list.get(j).getAcNum();
int iScore = list.get(i).getScore();
int jScore = list.get(j).getScore();
if (iAc < jAc) {
sort(i, j);
} else if (iAc == jAc) {
if (iScore > jScore) {
sort(i, j);
} else if (iScore == jScore) {
int compareTo = iName.compareTo(jName);
if (compareTo > 0) {
sort(i, j);
}
}
}
}
}
}
// 交換
public static void sort(int i, int j) {
int temp = list.get(i).getAcNum();
list.get(i).setAcNum(list.get(j).acNum);
list.get(j).setAcNum(temp);
temp = list.get(i).getScore();
list.get(i).setScore(list.get(j).score);
list.get(j).setScore(temp);
String string = list.get(i).getName();
list.get(i).setName(list.get(j).getName());
list.get(j).setName(string);
}
class person {
int acNum;
int score;
String name;
public person(int acNum, int score, String name) {
super();
this.acNum = acNum;
this.score = score;
this.name = name;
}
public void setAcNum(int acNum) {
this.acNum = acNum;
}
public void setScore(int score) {
this.score = score;
}
public void setName(String name) {
this.name = name;
}
public int getAcNum() {
return acNum;
}
public int getScore() {
return score;
}
public String getName() {
return name;
}
}
}