c语言来实现贪心算法之装箱问题

网友投稿 283 2023-08-03


c语言来实现贪心算法之装箱问题

装箱问题,贪心算法求近似最优解

复制代码 代码如下:

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]==0){

                            continhttp://ue;

  &nbmdpmbMsp;                     }

                        //将箱子加上去,原来位置清零

                        boxs[i]+=boxs[k];

                        int temp=boxs[k];

                        boxs[k]=0;

                        unEnchase--;

                        if(boxs[i]>boxCaptation){

                            //超过最大可容纳体积,状态复原

                            unEnchase++;

                            boxs[k]=temp;

                            boxs[i]-=boxs[k];

                            continue;

                        }

                        //最小箱子更新

                        if(k==minIndex){

                            for(int y=minIndex;y>0;y--){

                                if(boxs[y]!=0){

                                    minIndex=y;

                                }

                            }

                        }

      &nbspmdpmbM;                 break;

                    }

                }

            }

        }

        //统计箱子数

        int Boxcount=0;

        System.out.println("装箱结果:");

        for(int i=0;i

            System.out.print(boxs[i]+"\t");

            if(boxs[i]==0){

                continue;

            }

            Boxcount++;

        }

        System.out.println("\n箱子数:"+Boxcount);

    }

    phttp://ublic static void main(String[] args) {

        new Enchase().test1();

    }

}

以上就是本文的全部内容了,希望大家能够喜欢。

                //位置箱子重量为零跳过

                if(boxs[i]==0){

                    continue;

                }

                unEnchase--;

                while((boxCaptation-boxs[i])>=boxs[minIndex]){

                    int k=i+1;

                    for(;k>i;k++){

                        //位置箱子重量为零跳过

                        if(boxs[k]==0){

                            continhttp://ue;

  &nbmdpmbMsp;                     }

                        //将箱子加上去,原来位置清零

                        boxs[i]+=boxs[k];

                        int temp=boxs[k];

                        boxs[k]=0;

                        unEnchase--;

                        if(boxs[i]>boxCaptation){

                            //超过最大可容纳体积,状态复原

                            unEnchase++;

                            boxs[k]=temp;

                            boxs[i]-=boxs[k];

                            continue;

                        }

                        //最小箱子更新

                        if(k==minIndex){

                            for(int y=minIndex;y>0;y--){

                                if(boxs[y]!=0){

                                    minIndex=y;

                                }

                            }

                        }

      &nbspmdpmbM;                 break;

                    }

                }

            }

        }

        //统计箱子数

        int Boxcount=0;

        System.out.println("装箱结果:");

        for(int i=0;i

            System.out.print(boxs[i]+"\t");

            if(boxs[i]==0){

                continue;

            }

            Boxcount++;

        }

        System.out.println("\n箱子数:"+Boxcount);

    }

    phttp://ublic static void main(String[] args) {

        new Enchase().test1();

    }

}

以上就是本文的全部内容了,希望大家能够喜欢。

            System.out.print(boxs[i]+"\t");

            if(boxs[i]==0){

                continue;

            }

            Boxcount++;

        }

        System.out.println("\n箱子数:"+Boxcount);

    }

    phttp://ublic static void main(String[] args) {

        new Enchase().test1();

    }

}

以上就是本文的全部内容了,希望大家能够喜欢。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SuperSlide标签切换、焦点图多种组合插件
下一篇:java实现计算地理坐标之间的距离
相关文章

 发表评论

暂时没有评论,来抢沙发吧~