我正在开发一个简单的预订系统,我想根据机舱的容量为小组分配舱室,到目前为止,如果机舱没有容量,我可以将小组分成小组。整个小组。
<?php
$group = 14;
$cabinsArray = array("1"=>15,"2"=>3,"3"=>8);
$cantCab = count($cabinsArray);
echo "Available cabins: ".$cantCab."<br>";
$cabins = 0;
foreach($cabinsArray as $cabin => $capacity){
$group = $group - $capacity;
$cabins++;
if($group < 0){
break;
}
}
if($group > 0){
echo 'No cabins available, remains '.$grupo.' people out';
}else if($cabins > $cantCab){
echo 'No cabins available';
}else{
echo ceil($cabins);
}
?>
问题来自于它分配舱室,如果我的团队是24人,我的舱室容量订单是15,3,8而不是选择15和8它选择3舱。
如何根据容量选择舱室?
提前致谢
答案 0 :(得分:0)
这只是一个起点。该算法可能会更复杂,以弄清楚如何分割容量,但这是一个起点。您可以使用array_pop()
或end()
从$new
数组中获取最低值:
$group = 14;
$cabinsArray = array("32"=>3,"4"=>14,"200"=>8,"12"=>10,"6"=>4);
arsort($cabinsArray);
$cabinNums = array_keys($cabinsArray);
$cabinCaps = array_values($cabinsArray);
$highest = $cabinCaps[0];
$diff = $group - $highest;
$i = 0;
foreach($cabinCaps as $key => $value) {
if(($value >= $diff))
$new[$cabinNums[$key]] = $cabinCaps[$key];
$i++;
}
print_r($new);
print_r($highest);