溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用PHP怎么編寫一個裝箱算法

發(fā)布時間:2021-02-04 17:16:19 來源:億速云 閱讀:192 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)使用PHP怎么編寫一個裝箱算法,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

<?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; //共用盒子總數(shù)
$item_count = count( $items );
$box = array();//盒 子數(shù)組
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 );
?>

運(yùn)行結(jié)果:

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

關(guān)于使用PHP怎么編寫一個裝箱算法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI