题解 | HJ_68 #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
int sort = Integer.parseInt(in.nextLine());
List<Student> students = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
String[] student = in.nextLine().split(" ");
students.add(new Student(student[0], Integer.parseInt(student[1])));
}
students.stream().sorted(((o1, o2) -> {
if (sort == 0) {
return o2.score - o1.score;
} else {
return o1.score - o2.score;
}
})).forEach(System.out::println);
}
static class Student {
private String name;
private int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return name + " " + score;
}
}
}
SHEIN公司福利 826人发布