2020百度春招编程第二题


import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();    // 面额的种类数
        int m = sc.nextInt();    // 需要发放的工资总额
        ArrayList<Integer> money = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            int x = sc.nextInt();    // 面额、单位:元
            int y = sc.nextInt();    // 数目、单位:张
            for (int j = 0; j < y; j++) {
                money.add(x);    // 向金库中添加y张x元的钞票
            }
        }
        Collections.sort(money);    // 将金额按照从小到大的顺序排列
        
        // 开始发工资
        int months = pay(money, m);
        System.out.println(months);
    }

    static int pay(ArrayList<Integer> money, int m) {
        int month = 0;
        if (money.get(0) >= m) {
            month = money.size();    // 最小金额能够发放的话,直接发放
            return month;
        } else {
            // 最小金额不能发放,找到次小金额的下标
            int index = 1;
            while(money.get(0) == money.get(index)) index += 1;
            while ((money.get(0) + money.get(index)) >= m) {
                month += 1;
                money.remove(0);
                money.remove(index);
                if (money.get(index) >= m) {
                    return month + money.size() - index + 1;
                }
            }
        }
        return month;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值