成績排名(數組自定義排序)

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 Inputjava

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 Outputnode

Josephus    5  376
John        4  284
Alice       4  352
Smith       3  167
Bob         2  325
Bush        0    0

注意:題目沒有給出說明輸入的數量,因此輸入是直到文件的末尾
代碼:
import java.util.Scanner;
import java.util.Arrays;
import java.util.Scanner;
class node implements Comparable<node>{
    int  ac;
    int time;
    String name;
    @Override
    public int compareTo(node o) {
          if(this.ac!=o.ac)  return o.ac-this.ac;//從大到小
          else {
                if(this.time!=o.time)  return this.time-o.time;
                else  return this.name.compareTo(o.name);//從小到大
          }
    }
}
public class Main {
       public static void main(String[] args) {
             Scanner scan=new Scanner(System.in);
             int n=scan.nextInt();
             int m=scan.nextInt();
             node a[]=new node[10005];
             int i=0;
             while(scan.hasNext()){
                   a[i]=new node();
                   a[i].name=scan.next();
                   for(int j=0;j<n;j++){
                        String s=scan.next();
                        if(s.charAt(0)=='0') continue;
                        else if(s.charAt(0)=='-')  continue;
                        else if(s.charAt(0)>'0'&&s.charAt(0)<='9'){
                               a[i].ac++;
                               if(s.indexOf('(')<0){
                                      a[i].time+=Integer.parseInt(s);
                               }
                               else{
                                      String ls=s.substring(0,s.indexOf('('));
                                      a[i].time+=Integer.parseInt(ls);
                                      String rs=s.substring(s.indexOf('(')+1,s.indexOf(')'));
                                      a[i].time+=m*Integer.parseInt(rs);
                               }
                        }
                   }
                   i++;
             }
            Arrays.sort(a,0,i);
             for(int j=0;j<i;j++){//wrong在i
                 System.out.printf("%-10s%3d%5d",a[j].name,a[j].ac,a[j].time);
                 System.out.println();
             }
    }
}
相關文章
相關標籤/搜索