题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
int n = Integer.parseInt(in.nextLine());
int method = Integer.parseInt(in.nextLine());
List<String[]> list = new ArrayList<>();
while (n > 0) {
String[] item = in.nextLine().split(" ");
list.add(item);
n--;
}
sort(list, method);
}
}
private static void sort(List<String[]> list, int method) {
list.sort(new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
int v1 = Integer.parseInt(o1[1]);
int v2 = Integer.parseInt(o2[1]);
if (method == 0) {
int temp = v1;
v1 = v2;
v2 = temp;
}
if (v1 > v2) return 1;
else if(v1 < v2) return -1;
return 0;
}
});
for (String[] arr: list) {
System.out.println(arr[0] +" "+ arr[1]);
}
}
}

三奇智元机器人科技有限公司公司福利 70人发布