装箱问题,贪心算法求近似最优解
import java.util.Arrays;
import java.util.Comparator;
//装箱问题,贪心算法
public class Enchase {
public void test1() {
Integer[] boxs={34,6,40,2,23,12,12};
int boxCaptation=40;//箱子容量
//倒序
Arrays.sort(boxs, new Comparator() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
int unEnchase=boxs.length;//未装箱数
int minIndex=boxs.length-1;//最小的箱子指向
while (unEnchase>0) {
for(int i=0;i
//位置箱子重量为零跳过
if(boxs[i]==0){
continue;
}
unEnchase--;
while((boxCaptation-boxs[i])>=boxs[minIndex]){
int k=i+1;
for(;k>i;k++){
//位置箱子重量为零跳过
if(boxs[k]==