php - 将小元素装进大箱子

标签 php algorithm

我正在寻找一种解决方案来计算包装购物车产品所需的箱子数量。每个产品都有它的高度、宽度、长度。大盒子尺寸固定

//big box size
$max_height = 20;
$max_width = 30;
$max_length = 40;

foreach ($products as $product) {
    $height = $product->height;
    $width = $product->width;
    $length = $product->length;
}

我知道这是一个 3d 装箱问题,但是有没有其他更简单的方法来大致计算箱子的数量?

最佳答案

Ingo在原问题的评论中的解决方案很好,这里是它的实现。

//big box size
$max_height = 20;
$max_width = 30;
$max_length = 40;
$max_volume = $max_height * $max_width * $max_length;

$tot_height = 0;
$tot_width = 0;
$tot_length = 0;

foreach ($products as $product) {
    $tot_height += $product->height;
    $tot_width += $product->width;
    $tot_length += $product->length;
}

$tot_volume = $tot_height * $tot_width * $tot_length;

$boxes_needed = ceil($tot_volume / $max_volume);

如果你真的要在现实世界中使用它,那么可能会添加一个额外的盒子,因为它不太可能将所有东西组合在一起以完美地填充盒子体积,除非你是俄罗斯方 block 的大师:)

关于php - 将小元素装进大箱子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15177183/

相关文章:

php - 修改 Symfony Composer 供应商并使用它们直到他们的 PR 被 merge

php - 匹配带星号的字符串

java - 查找字符串 S2 所花费的时间

c# - 检查表达式中的循环依赖

PHP 数组键存在于字符串中

php - 在 PHP 中从远程数据库检索数据时出错

python - 优化蛮力数字求解器python的建议

javascript - 扫雷算法解决方案

php - Octobercms/Laravel 根据关系获取 id

algorithm - 改善图表中的总行驶距离