欢迎光临成都祈钰瑶科技有限公司网站!祝您生活愉快!
热线电话: 18982081108
✕

成都服务器托管,成都GPU服务器租用,成都高防BGP宽带租用,成都机柜租用托管

使用PHP怎么编写一个装箱算法-创新互联

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

成都创新互联公司是一家集成都网站制作、网站建设、网站页面设计、网站优化SEO优化为一体的专业网站建设公司,已为成都等多地近百家企业提供网站建设服务。追求良好的浏览体验,以探求精品塑造与理念升华,设计最适合用户的网站页面。 合作只是第一步,服务才是根本,我们始终坚持讲诚信,负责任的原则,为您进行细心、贴心、认真的服务,与众多客户在蓬勃发展的市场环境中,互促共生。
<?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怎么编写一个装箱算法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。