题解 | #成绩排序#

成绩排序

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);
        }
    }
}

全部评论
楼主思路很清晰啊,我都连上了
点赞 回复 分享
发布于 2023-05-30 09:15 陕西
这就是我和大佬的差距😶
点赞 回复 分享
发布于 2023-05-30 09:06 广东

相关推荐

不愿透露姓名的神秘牛友
07-10 14:00
林子大了什么鸟都有啊,我觉得我说的已经很客气了,阴阳谁呢
牛客62656195...:应该不是阴阳吧?你第一次注册的时候boss就说你是牛人
点赞 评论 收藏
分享
昨天 10:56
门头沟学院 Java
点赞 评论 收藏
分享
求offer的大角牛:不吃香菜
点赞 评论 收藏
分享
牛客38347925...:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:10
直接上图
牛客13578115...:改得一般,不值80
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务