猜测分箱算法

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

  

posted @ 2017-08-16 10:16  令狐る侠  阅读(324)  评论(0编辑  收藏  举报