题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.Scanner;
import java.util.TreeSet;
import java.util.Comparator;
import java.util.Arrays;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner (System.in);
String n = sc.nextLine();
String way = sc.nextLine();
Node[] arrayOfNode = new Node[Integer.parseInt(n)];
int i = 0;
String line ="";
while (sc.hasNextLine()){
line = sc.nextLine();
String[] array = line.split(" ");
Integer score = Integer.parseInt(array[1]);
Node node = new Node(array[0],score);
arrayOfNode[i] = node;
i++;
}
if (way.equals("1")){
Arrays.sort(arrayOfNode,new SL());
}
else {
Arrays.sort(arrayOfNode,new LS());
}
for (Node nd : arrayOfNode){
System.out.println(nd.toString());
}
}
public static class Node {
public String name;
public Integer score;
public Node(String n, Integer score){
name = n;
this.score = score;
}
public int smallToLarge(Node o) {
if (this.score>o.score){
return 1;
}
else {
return -1;
}
}
public String toString(){
return (name+" "+score);
}
}
public static class SL implements Comparator<Node>{
@Override
public int compare(Node o1, Node o2) {
return Integer.compare(o1.score,o2.score);
}
}
public static class LS implements Comparator<Node>{
@Override
public int compare (Node o1, Node o2){
return Integer.compare(o2.score,o1.score);
}
}
}

查看21道真题和解析