3 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊
編輯 將其更改$turn為$noin for 循環(huán)。您可以將其用于任何號碼。
<?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貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊
intval()向下舍入。您想要四舍五入,所以使用ceil().
$intnumber = ceil($numbertodivise / $no);
$rem = $numbertodivise % $intnumber;
$array = array_fill(0, $no, $intnumber);
if ($rem != 0) {
$array[count($array)-1] = $rem;
}

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊
無需循環(huán)。使用您的變量:
$count = ceil($numbertodivise / $no);
$rem = $numbertodivise - ($no * ($count-1));
$array = array_fill(0,$count-1,$no);
$array[] = $rem;
結(jié)果:
Array
(
[0] => 3
[1] => 3
[2] => 2
)
- 3 回答
- 0 關(guān)注
- 157 瀏覽
添加回答
舉報(bào)