【技术宅4】如何把M个苹果平均分给N个小朋友

  $apple=array('apple1','apple2','apple3','apple4','apple5','apple6','apple7','apple8','apple9','apple10','apple11','apple12','apple13','apple14');

  $children=array('chilren1','chilren2','chilren3','chilren4','chilren5','chilren6'); 

$i=0;

$chilren_count=count($children);

$count=count($apple);

 

while($count>0){      

       $index=mt_rand(0,count($apple)-1);   

       $tem=$apple[$index];   

       $fenpei[$children[$i]][]=$tem;     

      

       unset($apple[$index]);  

       $apple=array_values($apple);

       $count=count($apple);

      

       $i++;      

       if($i>$chilren_count-1){

              $i=0;

       }    

}

print_r($fenpei);

posted @ 2014-09-21 19:31  来自星星的bug  阅读(466)  评论(0编辑  收藏  举报