温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用PHP怎么编写一个装箱算法

发布时间:2021-02-04 17:16:19 来源:亿速云 阅读:172 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关使用PHP怎么编写一个装箱算法,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

<?php
//物品
$items[0] = 60;
$items[1] = 45;
$items[2] = 35;
$items[3] = 20;
$items[4] = 20;
$items[5] = 20;
$box_volume_count = 100; //每个盒 子的最大容积
$box_count = 0; //共用盒子总数
$item_count = count( $items );
$box = array();//盒 子数组
for ( $itemindex = 0; $itemindex < $item_count; $itemindex++ ) {
$_box_index = false;
$_box_count = count( $box );
for ( $box_index = 0; $box_index < $_box_count; $box_index++ ) {
 if ( $box[$box_index]['volume'] + $items[$itemindex] <= $box_volume_count ) {
 $_box_index = $box_index;
 break;
 }
}
if ( $_box_index === false ) {
 $box[$_box_count]['volume'] = $items[$itemindex];
 $box[$_box_count]['items'][] = $itemindex;
 $box_count++;
} else {
 $box[$_box_index]['volume'] += $items[$itemindex];
 $box[$_box_index]['items'][] = $itemindex;
}
}
print_r( $box );
?>

运行结果:

Array
(
    [0] => Array
        (
            [volume] => 95
            [items] => Array
                (
                    [0] => 0
                    [1] => 2
                )
        )
    [1] => Array
        (
            [volume] => 85
            [items] => Array
                (
                    [0] => 1
                    [1] => 3
                    [2] => 4
                )
        )
    [2] => Array
        (
            [volume] => 20
            [items] => Array
                (
                    [0] => 5
                )
        )
)

关于使用PHP怎么编写一个装箱算法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI