3 回答
TA贡献1831条经验 获得超9个赞
编辑 将其更改$turn为$noin for 循环。您可以将其用于任何号码。
<?php
$numbertodivide = 8;
$no = 3;
$array = [];
$added=0;//initialize the variable to track added number to make the given number divisible
while($numbertodivide%$no){
$numbertodivide+=1;
$added++;
}
$turn=$numbertodivide/$no;//get how many times we have to repeat the divider to get the given number
for($i=0;$i<$no-1;$i++){
$array[]=$turn;
}
$array[]=$turn-$added;//trim the added number from the last input of the number.
?>
TA贡献1853条经验 获得超6个赞
intval()向下舍入。您想要四舍五入,所以使用ceil().
$intnumber = ceil($numbertodivise / $no);
$rem = $numbertodivise % $intnumber;
$array = array_fill(0, $no, $intnumber);
if ($rem != 0) {
$array[count($array)-1] = $rem;
}
TA贡献1784条经验 获得超7个赞
无需循环。使用您的变量:
$count = ceil($numbertodivise / $no);
$rem = $numbertodivise - ($no * ($count-1));
$array = array_fill(0,$count-1,$no);
$array[] = $rem;
结果:
Array
(
[0] => 3
[1] => 3
[2] => 2
)
- 3 回答
- 0 关注
- 114 浏览
添加回答
举报