7-6 进步排行榜 (10 分)

7-6 进步排行榜 (10 分)
假设每个学生信息包括“用户名”、“进步总数”和“解题总数”。解题进步排行榜中,按“进步总数”及“解题总数”生成排行榜。要求先输入n个学生的信息;然后按“进步总数”降序排列;若“进步总数”相同,则按“解题总数”降序排列;若“进步总数”和“解题总数”都相同,则排名相同,但输出信息时按“用户名”升序排列。

输入格式:
首先输入一个整数T,表示测试数据的组数,然后是T组测试数据。每组测试数据先输入一个正整数n(1 < n < 50),表示学生总数。然后输入n行,每行包括一个不含空格的字符串s(不超过8位)和2个正整数d和t,分别表示用户名、进步总数和解题总数。

输出格式:
对于每组测试,输出最终排名。每行一个学生的信息,分别是排名、用户名、进步总数和解题总数。每行的各个数据之间留一个空格。注意,进步总数和解题总数都相同的学生其排名也相同。

输入样例:

1
6
usx15131 21 124
usx15101 27 191
usx15113 31 124
usx15136 18 199
usx15117 27 251
usx15118 21 124

输出样例:

1 usx15113 31 124
2 usx15117 27 251
3 usx15101 27 191
4 usx15118 21 124
4 usx15131 21 124
6 usx15136 18 199


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T  =scanner.nextInt();
        for (int w =0;w<T;w++){
            int n =scanner.nextInt();
            scanner.nextLine();//吃回车
            String name [] = new String[n];
            int improve [] = new int[n];
            int solve []= new int[n];
            //存数据
            for(int i =0;i<n;i++){

                String stings[]  =scanner.nextLine().split(" ");
                name[i] = String.valueOf(stings[0]);
                improve[i] = Integer.parseInt(stings[1]);
                solve[i] = Integer.parseInt(stings[2]);
            }
            for(int i =0;i<n;i++){
                for(int j =0 ;j < n - i - 1;j++){
                    if(improve[j] < improve[j+1] ){
                        int tem = improve[j+1];
                        improve[j+1] = improve[j];
                        improve[j] = tem;

                        int te = solve[j+1];
                        solve[j+1] = solve[j];
                        solve[j] = te;

                        String st = String.valueOf(name[j+1] );
                        name[j+1] = String.valueOf(name[j]);
                        name[j] = String.valueOf(st);
                    }
                    if(improve[j] == improve[j+1]){
                        if(solve[j] < solve[j+1]){
                            int tem = improve[j+1];
                            improve[j+1] = improve[j];
                            improve[j] = tem;

                            int te = solve[j+1];
                            solve[j+1] = solve[j];
                            solve[j] = te;

                            String st = String.valueOf(name[j+1] );
                            name[j+1] = String.valueOf(name[j]);
                            name[j] = String.valueOf(st);
                        }
                        if(solve[j] == solve[j+1]){
                            if(name[j].compareTo(name[j+1]) > 0){
                                int tem = improve[j+1];
                                improve[j+1] = improve[j];
                                improve[j] = tem;

                                int te = solve[j+1];
                                solve[j+1] = solve[j];
                                solve[j] = te;

                                String st = String.valueOf(name[j+1] );
                                name[j+1] = String.valueOf(name[j]);
                                name[j] = String.valueOf(st);
                            }
                        }
                    }
                }
            }
            int i = 0;
            while (i<n) {

                int num1 = improve[i];
                int num2 = solve[i];
                int cur = i ;
                System.out.println(i+1+" " + name[i]+" "+improve[i]+" "+solve[i]);
                while (i + 1 < n) {
                    if (num1 == improve[i +1]  && num2 == solve[i+1]) {
                        System.out.println(cur+1+" " + name[i + 1]+" "+improve[i+1]+" "+solve[i+1]);
                        i++;

                    } else {
                        break;
                    }
                }
                i++;
               
            }
        }

    }
}

你可能感兴趣的:(Java,java)