首页 > 编程语言 > 详细

猜测分箱算法

时间:2017-08-16 10:51:45      阅读:293      评论:0      收藏:0      [点我收藏+]
public static void main(String[] args) {

    List<Integer> tax = new ArrayList<>();
    tax.add(70);
    tax.add(55);
    tax.add(40);
    tax.add(30);
    tax.add(20);
    tax.add(10);

    // 关税倒序排列
    Collections.sort(tax, new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2.compareTo(o1);
        }
    });

    int bagCnt = 0;
    int taxSum = 0;
    int bagLmt = 50;

    for (Integer t : tax) {
        if (t >= bagLmt) {
            bagCnt++;
            taxSum = 0;
        } else {
            if (taxSum + t > bagLmt) {
                bagCnt++;
                taxSum = t;
            } else {
                taxSum += t;
            }
        }
    }
    if(taxSum >0) bagCnt++;

    System.out.println(bagCnt);
}

  

猜测分箱算法

原文:http://www.cnblogs.com/dreammyone/p/7371903.html

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