首页 > 其他 > 详细

2020百度春招编程第二题

时间:2020-03-14 21:57:03      阅读:107      评论:0      收藏:0      [点我收藏+]
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;
    }
}

2020百度春招编程第二题

原文:https://www.cnblogs.com/zhyantao/p/12494580.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!