算法-最优算法-背包算法-合并订单-最优解

1 篇文章 0 订阅
1 篇文章 0 订阅

算法最优算法 背包算法。

工作中遇到的问题是:订单合成运单这个是最优解

前言
工作中遇到一个类似这样的问题
自动将订单,合并成运单。
需求:用户点一下自动按照规则生成 运单。

这个是雏形。

有什么疑问可以留言一起讨论。

算法效果

{1,5,3,8,5,8,5} 找出 组合不重复的 最接近 15 的 组合
程序会先整理,数组为:[1, 3, 5, 5, 5, 8, 8] ,程序结果: 15- 下标:[2, 3, 4]

图意:

在这里插入图片描述

Java代码如下:

package top.isst.test;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author isst
 * @date 2021/4/16 11:41
 */
public class Test2StusA {
    public static void main(String[] args) {

//        int[] arr = {1,5,3,8,5,8,9,6,5,2};
//  会在  arr 数组中找出   最接近 max组合   如:arr[1] arr[4] arr[6] 这样的输出
        int max = 15;
        int[] arr = {1,5,3,8,5,8,5};
        Arrays.sort(arr);
        System.out.println("整理后的数组为:" + Arrays.toString(arr));
        System.out.println("下标以整理后的数组为准。");
//        int[] arr = {1,1,1};
//        int max = 2;
        MathEntity[] end = getArray(arr,max);
        for (int i = 0; i < end.length; i++) {
            if(!end[i].getVa().equals(new BigDecimal(0))){
             System.out.println(end[i].getVa()+"- 下标:"+Arrays.toString(end[i].getIndex()));
//                System.out.println(end[i].getVa()+"-");
            }
        }
    }

    public static MathEntity[] getArray(int[] arr,int max){
        //定义容器 结构
        List<MathEntity> re = new ArrayList<>();
        for (int i = 0; i <arr.length ; i++) {
            for (int j = 0; j <arr.length ; j++) {
                MathEntity mathEntity=new MathEntity();
                Set<Integer> index= new HashSet<>();
                int gezh=0;
                for (int k = i; k < arr.length-j; k++) {
                    if(arr[k]>max){
                        break;
                    }
                    gezh+=arr[k];
                    index.add(k);
                }
               // System.out.print(gezh+"-"+Arrays.toString(index.toArray(new Integer[0]))+"\t");
                if(gezh <=max){
                    mathEntity.setVa(BigDecimal.valueOf(gezh));
                    mathEntity.setIndex(index.toArray(new Integer[0]));
                    re.add(mathEntity);
                }
            }
//            System.out.println();
        }
        Collections.sort(re );
        return re.toArray(new MathEntity[0]);
    }
}
package top.isst.test;

import java.math.BigDecimal;

/**
 * @author isst
 * @date 2021/4/16 11:43
 */
public class MathEntity  implements Comparable<MathEntity>{
    private BigDecimal va =new BigDecimal(0);
    private Integer[] index;
    public BigDecimal getVa() {
        return va;
    }
    public void setVa(BigDecimal va) {
        this.va = va;
    }
    public Integer[] getIndex() {
        return index;
    }
    public void setIndex(Integer[] index) {
        this.index = index;
    }
    //实现接口方法,将来排序的时候sort看正负数还是零来进行判断大小
    @Override
    public int compareTo(MathEntity arg0) {
        return arg0.getVa().compareTo( this.getVa());      //这里定义你排序的规则。
    }
}

运行结果

如果你需要一组 取其中的 MathEntity[] end 第一个元素

整理后的数组为:[1, 3, 5, 5, 5, 8, 8]
下标以整理后的数组为准。
15- 下标:[2, 3, 4]
14- 下标:[0, 1, 2, 3]
13- 下标:[1, 2, 3]
13- 下标:[4, 5]
10- 下标:[2, 3]
10- 下标:[3, 4]
9- 下标:[0, 1, 2]
8- 下标:[1, 2]
8- 下标:[5]
8- 下标:[6]
5- 下标:[2]
5- 下标:[3]
5- 下标:[4]
4- 下标:[0, 1]
3- 下标:[1]
1- 下标:[0]

欢迎关注我的公众号高质量博文等你看

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

isst

我不喝咖啡,我想喝瓶水。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值