定制排序

爱新觉罗LQ / 2023-09-05 / 原文

定制排序

1. 冠亚军排名

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = Integer.parseInt(in.nextLine());
        List<medal> list = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            String[] split = in.nextLine().split(" ");
            String country = split[0];
            int goldCount = Integer.parseInt(split[1]);
            int silverCount = Integer.parseInt(split[2]);
            int bronzeCount = Integer.parseInt(split[3]);
            list.add(new medal(country, goldCount, silverCount, bronzeCount));
        }
        list.stream().sorted((o1, o2) -> {
            int goldCount1 = o1.goldCount;
            int goldCount2 = o2.goldCount;
            int silverCount1 = o1.silverCount;
            int silverCount2 = o2.silverCount;
            int bronzeCount1 = o1.bronzeCount;
            int bronzeCount2 = o2.bronzeCount;
            if (goldCount1 !=  goldCount2){
                return goldCount2 - goldCount1;
            }
            if (silverCount1 != silverCount2){
                return silverCount2 - silverCount1;
            }
            if (bronzeCount1 != bronzeCount2){
                return bronzeCount2 - bronzeCount1;
            }
            return o1.country.compareTo(o2.country);
        }).forEach(medal -> System.out.println(medal.country));
    }
}

class medal{
    String country;
    int goldCount;
    int silverCount;
    int bronzeCount;

    public medal(String country, int goldCount, int silverCount, int bronzeCount) {
        this.country = country;
        this.goldCount = goldCount;
        this.silverCount = silverCount;
        this.bronzeCount = bronzeCount;
    }
}