最近工作中需要处理一个实际问题就是大箱子装小盒子的问题,写这篇文章需要解决的实际问题 就是大容器装小东西的问题。例如仓库中货位装载SKU 车厢里面装载快递包裹。
以下代码实现比较粗糙,实际过程中就是为了计算一个箱子中能装多少东西
前提条件
1、自己校验完 最长比较,即 小东西的 L(length) 、W(width)、 H(height) 最大值不能超过 大容器的LWH, 本文只讨论 立体箱子问题 杠精 读到这里就可以去逛其他帖子了,避免浪费你宝贵的时间。
大箱子 小盒子 基本属性类
CountBox
@Data
@AllArgsConstructor
public class CountBox {
private Integer length;
private Integer width;
private Integer height;
public Double getVol()
{
return (double) this.length*this.width*this.height;
}
}
对于计算 结合前人的计算方法 计算了一个朴素的算法 得到的结果不一定对 后面读者有更好的方式 ,请留言 让鄙人改进 以更好的适应
具体的方法类(可能个人习惯了Java 或者Spring 的注入特性的习惯,公共的方法就抽象出来做某一类业务或者某一种方法)
@Slf4j
public class CountUtils {
/**
* 朴素计算(不计算之前已经装了的体积) 大箱子能装多少个小箱子
* @param maxBox
* @param minBox
* @return
* 原理是:
* nx < X
* ny < Y
* nz < Z
* 交换 XYZ 比较顺序来计算各种方法最多能放多少个 取整乘积最大值就是可放置的最多个数(朴素算法)
*/
public Integer toCount(CountBox maxBox,CountBox minBox)
{
int a = (
(maxBox.getLength() / minBox.getLength()) *
(maxBox.getWidth() / minBox.getWidth()) *
(maxBox.getHeight() / minBox.getHeight())
);
int b = (
(maxBox.getLength() / minBox.getLength()) *
(maxBox.getWidth() / minBox.getHeight()) *
(maxBox.getHeight() / minBox.getWidth())
);
int c = (