题解 | #成绩排序#
成绩排序
http://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.*;
public class Main { //采用id数组来记录分数排序后的位置
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = Integer.valueOf(sc.nextLine());
int index = Integer.valueOf(sc.nextLine());
int [][] arr2 = new int[count][2];
ArrayList<String> arry = new ArrayList<>();
for (int i = 0 ; i < count; i++) {
arry.add(sc.nextLine());
String [] str = arry.get(i).split(" ");
arr2[i][0] = Integer.valueOf(str[1]) ;//获取分数;
arr2[i][1] = i; //获取id;
}
int temp1 = count -1;
int temp2 = count-1;
if(index == 1){//采用冒泡算法,来排序分数,能使得相同分数按输入顺序排列;
for(int i = 0; i< temp1 ; i++){
for(int j = 0; j< temp2; j++){
if(arr2[j][0] > arr2[j+1][0]){
int temp = arr2[j][0];
int Temp = arr2[j][1];
arr2[j][0] = arr2[j+1][0];
arr2[j+1][0] =temp;
arr2[j][1] = arr2[j+1][1];
arr2[j+1][1] =Temp;
}
}
}
}
if(index ==0){
for(int i = 0; i< temp1 ; i++){
for(int j = 0; j< temp2; j++){
if(arr2[j][0] < arr2[j+1][0]){
int temp = arr2[j][0];
int Temp = arr2[j][1];
arr2[j][0] = arr2[j+1][0];
arr2[j+1][0] =temp;
arr2[j][1] = arr2[j+1][1];
arr2[j+1][1] =Temp;
}
}
}
}
for(int i=0; i<count; i++){//根据分数排序的ID,获取输出顺序;
System.out.println(arry.get(arr2[i][1]));
}
}
}