题解 | 分数线划定-Java

分数线划定

https://www.nowcoder.com/practice/2395fa7b6c6e452e8d8310a7cfdbe902

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        int t = (int) (m * 1.5);
        List<Candidate> candidates = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int k = scanner.nextInt();
            int s = scanner.nextInt();
            candidates.add(new Candidate(k, s));
        }

        // 排序:成绩从高到低,成绩相同报名号从小到大
        // Override Comparator比较器
        candidates.sort((o1, o2)-> {
            if (o1.score != o2.score) return o2.score - o1.score;
            else return o1.id - o2.id;
        });

        int line = candidates.get(t - 1).score;
        int cnt = 0;
        List<Candidate> interviewCandidates = new ArrayList<>();
        for (Candidate candidate : candidates) {
            if (candidate.score >= line) {
                interviewCandidates.add(candidate);
                cnt++;
            }
        }

        System.out.println(line + " " + cnt);
        for (Candidate candidate : interviewCandidates) {
            System.out.println(candidate.id + " " + candidate.score);
        }

        scanner.close();
    }
}

class Candidate {
    int id;
    int score;

    public Candidate(int id, int score) {
        this.id = id;
        this.score = score;
    }
}

全部评论

相关推荐

迷茫的大四🐶:看来已经准备换人了
点赞 评论 收藏
分享
大世界中的渺小一棵:看出来你软硬都有基础,但是这样写简历软硬都擦边不知道你想投什么,建议针对岗位jd针对性修改下。
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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