杭電oj2093題,Java版

杭電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();
    }
}