面试问题:发一个随机红包,100块钱给10个人。每个人最多12块钱,最少6块钱。怎么分?

以前想过一个类似问题,就是没有每个人最大、最小的得钱数的限制,以前的问题可以很好用随机数解决。

于是这个问题也被以前的思想带坑里了,把突破口完全放在了如何处理每个人的随机数上。

于是在面试时间就没有解决这个问题,直到面试结束自己安静下来,仔细想想,发现思路错了。

我认为正确的思路是:每个人先得6块钱,这样剩下40块钱,之后每次拿出一块钱,随机分配给一个人,如果某个人的钱数达到了上限,那么这个人下次就没有了再得到钱的资格了。这样直到剩下钱都分配完。

当然在接口的实际处理上可以做些优化,例如剩下的钱每次随机分配的钱可以是随机的(当然这个随机要做一些限制,以免一下就分配超额了),然后如果某个人钱+这次随机分配的钱>每个人的上限,那么他就没有资格得到这个钱了。

随机分配也好实现,先算有几个人有资格得到这笔钱,随即一个数,决定给第几个符合资格的人。

我的思路就是这样,大家如果有更好的思路,请告知。谢谢。


森栏
浏览 1094回答 2
2回答

临摹微笑

function microtime_float(){&nbsp; &nbsp; list($usec, $sec) = explode(" ", microtime());&nbsp; &nbsp; return ((float)$usec + (float)$sec);}function getRandParcent(){&nbsp; &nbsp; return rand(1,10)/rand(10,100); &nbsp;}function randUserMoney($cash,$min=6,$max=12){&nbsp; &nbsp; $cash_ini = $cash;&nbsp; &nbsp; $user_arr = array($min,$min,$min,$min,$min,$min,$min,$min,$min,$min);&nbsp; &nbsp; $start = microtime_float();&nbsp; &nbsp; while($cash>0){&nbsp; &nbsp; &nbsp; &nbsp; $user_id = rand(0, 9);&nbsp; &nbsp; &nbsp; &nbsp; $rand_point = getRandParcent();&nbsp; &nbsp; &nbsp; &nbsp; if($user_arr[$user_id]<$max){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ing = microtime_float();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($ing-$start>0.01){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return randUserMoney($cash_ini);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rand_money = round($rand_point*$cash,2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $user_money = $user_arr[$user_id]+$rand_money ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($user_money<$max){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $user_arr[$user_id] = $user_money;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $cash = $cash - $rand_money;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return [&nbsp; &nbsp; 'user_money'=>$user_arr,&nbsp; &nbsp; 'total_money'=>array_sum($user_arr),&nbsp; &nbsp; 'excute_time'=>$ing-$start&nbsp; &nbsp; ];}var_dump(randUserMoney(40));array (size=3)&nbsp; 'user_money' =>&nbsp;&nbsp; &nbsp; array (size=10)&nbsp; &nbsp; &nbsp; 0 => float 11.59&nbsp; &nbsp; &nbsp; 1 => float 9.07&nbsp; &nbsp; &nbsp; 2 => float 11.99&nbsp; &nbsp; &nbsp; 3 => float 12&nbsp; &nbsp; &nbsp; 4 => float 9.14&nbsp; &nbsp; &nbsp; 5 => float 11.6&nbsp; &nbsp; &nbsp; 6 => float 11.86&nbsp; &nbsp; &nbsp; 7 => float 9.93&nbsp; &nbsp; &nbsp; 8 => float 6&nbsp; &nbsp; &nbsp; 9 => float 6.82&nbsp; 'total_money' => float 100&nbsp; 'excute_time' => float 0.004000186920166

慕码人8056858

$cash = 40;$user_arr = array(6,6,6,6,6,6,6,6,6,6);while($cash>0){&nbsp; &nbsp; $user_id = rand(0, 9);&nbsp; &nbsp; if($user_arr[$user_id]<12){&nbsp; &nbsp; &nbsp; &nbsp; $user_arr[$user_id]++;&nbsp; &nbsp; &nbsp; &nbsp; $cash--;&nbsp; &nbsp; }};var_dump($user_arr,array_sum($user_arr));die;性能篇$arr1=range(2,6);shuffle($arr1);$arr2=range(2,6);shuffle($arr2);$user_arr = array(6,6,6,6,6,6,6,6,6,6);&nbsp;for ($i=0;$i<10;$i++){&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;if($i<=4){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$user_arr[$i] += $arr1[$i];&nbsp; &nbsp; &nbsp;}else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$j = $i%5;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$user_arr[$i] += $arr2[$j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;}&nbsp;}var_dump($user_arr,array_sum($user_arr));die;
打开App,查看更多内容
随时随地看视频慕课网APP