Php 分配一个没有余数的数字
我想在不切割多余部分的情况下,将一个项目分配到特定的部分, 例如:Php 分配一个没有余数的数字,php,arrays,division,Php,Arrays,Division,我想在不切割多余部分的情况下,将一个项目分配到特定的部分, 例如: $persons=7; $rooms=2; 对于($i=0;$i0) // { //$the_room[$index++]++; // } 打印室(房间); 如果您希望按顺序共享剩余部分,请添加“顺序分发”代码块。 $persons = 7; $rooms = 2; $the_room = array(); for($i = 0; $i < $rooms; $i++) { $the_room[$i] = floor(
$persons=7;
$rooms=2;
对于($i=0;$i<$rooms;$i++)
{
//分配每个房间的人数
}
//端点应该是
$the_room[0]=4//而不是3.5
$the_room[1]=3//而不是3.5
一种先平均分配片段,然后随机分配剩余片段的方法怎么样
$persons = 7;
$rooms = 2;
$the_room = array();
for($i = 0; $i < $rooms; $i++)
{
$the_room[$i] = floor($persons / $rooms);
}
// Random Distribution
while( $persons - array_sum($the_room) > 0)
{
$the_room[array_rand($the_room)]++;
}
// Sequential Distribution
// $index = 0;
// while( $persons - array_sum($the_room) > 0)
// {
// $the_room[$index++]++;
// }
print_r($the_room);
$persons=7;
$rooms=2;
$the_room=array();
对于($i=0;$i<$rooms;$i++)
{
$the_room[$i]=楼层($persons/$rooms);
}
//随机分布
而($persons-array\u sum($the\u room)>0)
{
$the_room[array_rand($the_room)]++;
}
//顺序分布
//$index=0;
//而($persons-array\u sum($the\u room)>0)
// {
//$the_room[$index++]++;
// }
打印室(房间);
如果您希望按顺序共享剩余部分,请添加“顺序分发”代码块。
$persons = 7;
$rooms = 2;
$the_room = array();
for($i = 0; $i < $rooms; $i++)
{
$the_room[$i] = floor($persons / $rooms);
}
// Random Distribution
while( $persons - array_sum($the_room) > 0)
{
$the_room[array_rand($the_room)]++;
}
// Sequential Distribution
// $index = 0;
// while( $persons - array_sum($the_room) > 0)
// {
// $the_room[$index++]++;
// }
print_r($the_room);