我的脚本中有以下简单的除法计算:
<?php
$total = 1001;
$people = 5;
$division= $total / $people;
echo "division = ".$division;
?>
输出division = 200.2
。
是否可以像下面这样存储它?
$division[0] = 200;
$division[1] = 200;
$division[2] = 200;
$division[3] = 200;
$division[4] = 201;
最佳答案
// initital conditions
$total = 1001;
$people = 5;
// count what is the minimal value, that all the results will have
$value = floor($total / $people);
$result = array_fill(0, $people, $value);
// distribute missing "+1"s as needed in the result
$overheads = $total - $value * $people;
for ($i = 0; $i < $overheads; $i++)
$result[$i]++;
// voila...
var_dump($result);
关于php - 将除法计算结果存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42670545/