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
注意:题目没有给出说明输入的数量,所以输入是直到文件的末尾
代码:
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(); } } }
原文:https://www.cnblogs.com/qdu-lkc/p/12192036.html