问一个算法的问题????? 我要裁各种长度的料,比方我要裁120,120,150,150,150,140,75,75。料是600的,所以我要150,150,150,75,75先出,然后120,120,140。就是要相加最接近600先出,用J2SE能做不?谁帮帮忙呀。。谢谢啦 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 我上面说的长度只是做个说明,按你给的第一根是540,第二根是440,就是剩了一个60和160,这不是浪费了吗,因为整根的料是600。如果先出600,后面就剩220的料。这样就会好些。因为剩的60可能因为太短我用不了了。感觉这个真是不好做,因为可能要裁的料有时候有几百根。有时候不是某两根而是某N根的和<剩下的 为什么一个java文件只能存在一个public类? 子类调用父类的方法一定会有super吗? 关于鼠标焦点的问题,请来看看~~ 关于Vector赋值取值问题 这个程序片段输出的结果 之前提的问题,还是没解决,swing中中英文并存的问题 菜鸟问题,急需各位大侠给予指教!这是一个关于向access数据库写入数据的问题,非常奇怪,从数据库读数据,一切正常,写时就出问题了,请 急救:关于数据库的更新问题 java中表示时间的类型是什么? 请问谁有《JBuilder6实例入门》,海洋出版社的,黄色封面,我的光盘坏了一部分,求救! 请教一个正则表达式 新手问题。。。及及
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