杭电oj2093题,Java版

时间:2022-11-07 17:04:30

杭电2093题,Java版

虽然不难但很麻烦。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner; /**
* @Author Yun
* @Date 2020/9/9 8:56
*
* Accepted!!!!!
*/
class node implements Comparable<node>{
String name;
int num;
int time; public node(String name, int num, int time) {
this.name = name;
this.num = num;
this.time = time;
} @Override
public int compareTo(node o) {
if(this.num<o.num){ //num 降序
return 1;
}else if(this.num>o.num){
return -1;
}else {
if(this.time>o.time){
return 1;
}else if(this.time<o.time){
return -1;
}else {
return this.name.compareTo(o.name)>0?1:-1;
}
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<node> list = new ArrayList<>();//记录每个学生的姓名、AC数和time
int n = sc.nextInt();
int m = sc.nextInt(); while (sc.hasNext()){
String name = sc.next(); int time = 0,num = 0;
for (int k = 0; k < n; k++) {
String s = sc.next();
if(s.charAt(0)=='-'|| s.equals("0")){
continue;
}else if(s.contains("(")){
num ++; //AC数加一 //计算用时
int tmp = 0;
int i = 0;
for (i = 0; s.charAt(i)!='(' ; i++) {
tmp = tmp*10+(s.charAt(i)-'0');
}
time += tmp; //计算罚时
tmp = 0;
for (int j = i+1; s.charAt(j)!=')'; j++) {
tmp = tmp*10+(s.charAt(j)-'0');
}
time+=tmp*m;
}else {
int tmp = 0;
for (int i = 0; i<s.length(); i++) {
tmp = tmp*10+(s.charAt(i)-'0');
}
num++;
time+=tmp;
}
} //建node
node nodex = new node(name,num,time);
list.add(nodex);
}
Collections.sort(list); //排序 //按要求输出
for (node nodex:list) {
System.out.printf("%-10s%3d%5d%n",nodex.name,nodex.num,nodex.time);
}
sc.close();
}
}