我要裁各种长度的料,比方我要裁120,120,150,150,150,140,75,75。料是600的,所以我要150,150,150,75,75先出,然后120,120,140。就是要相加最接近600先出,用J2SE能做不?谁帮帮忙呀。。谢谢啦

解决方案 »

  1.   

    import java.util.ArrayList;public class QC {    static ArrayList als = new ArrayList();    static ArrayList permutation(ArrayList al, ArrayList outal) {
            if (al.isEmpty()) {
                als.add(outal);
            } else {
                for (int i = 0; i < al.size(); i++) {
                    ArrayList hOutal = (ArrayList) outal.clone();
                    hOutal.add(al.get(i));
                    ArrayList hal = (ArrayList) al.clone();
                    hal.remove(i);
                    permutation(hal, hOutal);
                }
            }        return als;
        }    static void listprint(ArrayList al, int l) {        System.out.println("\n第一根");
            int d = 0;
            for (Object s : al) {
                d += (Integer) s;            if (l <= d) {
                    d = 0;
                    System.out.println("\n下一根");            }
                System.out.print((Integer) s + " ");        }
        }    public static void main(String[] args) {
            ArrayList test = new ArrayList();        int l = 600;
            test.add(120);//120,120,150,150,150,140,75,75
            test.add(120);
            test.add(150);
            test.add(150);
            test.add(150);
            test.add(75);
            test.add(140);
            test.add(75);//初始数据的啊        ArrayList allAL = permutation(test, new ArrayList());        int i = 0, b = 0, sum = 0, nsum = 0, minsum = 0;
            ArrayList<String> sumal = new ArrayList<String>();        for (Object al1 : allAL) {
                for (Object al2 : (ArrayList) al1) {
                    nsum += (Integer) al2;
                    if (nsum >= l) {
                        nsum = 0;
                        sum++;
                    }
                }
                i++;
                if (sum < minsum) {
                    minsum = sum;
                    b = i;
                }
            }
            listprint((ArrayList) (allAL.get(b)), l);
        }
    }第一根
    120 120 150 150 
    下一根
    150 75 140 75
      

  2.   

    我上面说的长度只是做个说明,按你给的第一根是540,第二根是440,就是剩了一个60和160,这不是浪费了吗,因为整根的料是600。如果先出600,后面就剩220的料。这样就会好些。因为剩的60可能因为太短我用不了了。感觉这个真是不好做,因为可能要裁的料有时候有几百根。有时候不是某两根而是某N根的和<剩下的