码迷,mamicode.com
首页 > 编程语言 > 详细

猜测分箱算法

时间:2017-08-16 11:34:04      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:[]   sum   ++   void   bag   tor   print   static   class   

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);
}

  

猜测分箱算法

标签:[]   sum   ++   void   bag   tor   print   static   class   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!