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
Josephus 5 376 John 4 284 Alice 4 352 Smith 3 167 Bob 2 325 Bush 0 0
import java.util.Scanner; public class P2093 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); int m=sc.nextInt(); Person[] p=new Person[1000]; int k=0; while(sc.hasNext()){// String name=sc.next(); int time=0,num=0; for(int i=0;i<n;i++){ int timer; int numb=0; String s=sc.next(); String[] str=s.split("\\(|\\)");//这里注意“()”在java里要有双\\,卡这了 呜呜呜 这个就是java的优势,可以直接用这个分隔出来 if(str.length==1){ timer=Integer.parseInt(s);//然后通过Integer里面的parseInt方法直接将字符串转换为整数 }else{ timer=Integer.parseInt(str[0]); numb=Integer.parseInt(str[1]); } if(timer>0){ num++; time+=timer+numb*m; } } p[k++]=new Person(name,num,time); //System.out.println("num="+num+" time="+time); } sort(p,k); for(int i=0;i<k;i++){ System.out.printf("%-10s %2d %4d",p[i].name,p[i].num,p[i].time); System.out.println(); } } } private static void sort(Person[] p,int k){ for(int i=0;i<k-1;i++){ for(int j=0;j<k-1-i;j++){ if(p[j].num<p[j+1].num){ swap(p,j,j+1); }else if(p[j].num==p[j+1].num){ if(p[j].time>p[j+1].time){ swap(p,j,j+1); }else if(p[j].time==p[j+1].time){ if(p[j].name.compareTo(p[j+1].name)>0){ swap(p,j,j+1); } } } } } } private static void swap(Person[] p, int j, int i) { Person temp=new Person(); temp=p[j]; p[j]=p[i]; p[i]=temp; } } class Person{ public String name; public int num; public int time; public Person(String name,int num,int time){ this.name=name; this.num=num; this.time=time; } public Person(){ } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u011479875/article/details/47194285