05:分数线划定

一、题目链接

http://noi.openjudge.cn/ch0110/05/

二、解题思路

三、实施步骤

四、Java程序

import java.util.Arrays;
import java.util.Scanner;

class Volunteer implements Comparable<Volunteer> { // 志愿者信息类,实现Comparable接口
    int id; // 志愿者报名号
    int score; // 志愿者笔试成绩

    public Volunteer(int id, int score) { // 构造方法,用于初始化一个志愿者对象
        this.id = id;
        this.score = score;
    }

    /* 重写Comparable接口的compareTo方法,按score主属性降序、id次属性升序排序 */
    @Override
    public int compareTo(Volunteer o) {
        if (this.score != o.score) { // 如果两个志愿者的笔试成绩不相等
            return o.score - this.score; // 按score属性降序排序
        }
        return this.id - o.id; // 按id属性升序排序
    }

    /* 重写Object类的toString方法,满足题目要求的输出格式 */
    @Override
    public String toString() {
        return id + " " + score;
    }
}

public class Main {
    /**
     * 计算并返回面试分数线和实际面试人数
     *
     * @param volunteers Volunteer类型的数组,存储所有志愿者记录
     * @param plan       int类型的整数,代表计划录取人数
     * @return int类型的数组,包含面试分数线和实际面试人数
     */
    public int[] interview(Volunteer[] volunteers, int plan) {
        int n = volunteers.length; // 报名人数
        int number = (int) (Math.floor(plan * 1.5)); // 计划面试的人数
        int line; // 面试分数线
        Arrays.sort(volunteers); // 根据Volunteer类的设定规则对volunteers数组排序
        line = volunteers[number - 1].score; // 获取面试分数线
        /* 如有压线重分现象,需要增加面试人数 */
        while (number < n && volunteers[number].score == line) {
            number++;
        }
        return new int[]{line, number}; // 返回面试分数线和实际面试人数
    }

    public static void main(String[] args) {
        Main test = new Main();
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int m = input.nextInt();
        Volunteer[] volunteers = new Volunteer[n];
        for (int i = 0; i < n; i++) {
            int id = input.nextInt();
            int score = input.nextInt();
            volunteers[i] = new Volunteer(id, score);
        }
        int[] ans = test.interview(volunteers, m);
        System.out.println(ans[0] + " " + ans[1]);
        for (int i = 0; i < ans[1]; i++) {
            System.out.println(volunteers[i].toString());
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江苏科技大学_计算机学院_潘磊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值