gpt4 book ai didi

php - 将小元素装进大箱子

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:25:41 25 4
gpt4 key购买 nike

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

//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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com