题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = in.nextInt();
        int sortNum = in.nextInt();
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= count; i++) {
            String name = in.next();
            int score = in.nextInt();
            list.add(i + "_" + name + "_" + score);
        }

        list.sort((o1, o2) -> {
            int i1 = o1.lastIndexOf("_");
            int i2 = o2.lastIndexOf("_");
            if (sortNum == 1) {
                return Integer.parseInt(o1.substring(i1 + 1)) - Integer.parseInt(o2.substring(
                            i2 + 1));
            } else {
                return Integer.parseInt(o2.substring(i2 + 1)) - Integer.parseInt(o1.substring(
                            i1 + 1));
            }
        });

        list.forEach(o -> {
            int i1 = o.indexOf("_");
            int i2 = o.lastIndexOf("_");
            System.out.println(o.substring(i1 + 1, i2) + " " + o.substring(i2 + 1));
        });
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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