您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

帮帮看看这个简单的算法

2025/5/25 18:18:34发布10次查看
写的不好,求大家优化
一个用户id的数组
$uids = [1,2,3,5,6,8,9,11,13,25,65];

这个数组的每一个键值代表一个uid
金额数组
$amounts = [ 1=>12000, 2=>500, 3=>11000, 5=>1000, 6=>11000, 8=>12000, 9=>12000, 11=>11000, 13=>12000, 25=>22000, 65=>123123];

此数组的键名和$uid数组的键值对应。
循环$uids这个数组,取出对应的金额$amount,
如果这个金额大于等于12000($boundary),就在总金额($totals)加上这个$amount
如果这个金额小于12000,就再向下循环,循环到这几个$amount相加大于等于12000,然后在总金额($totals)加上这几个$amount的和
最多取三层。
最后得出$totals的值。
我现在的代码:
12000, 2=>500, 3=>11000, 5=>1000, 6=>11000, 8=>12000, 9=>12000, 11=>11000, 13=>12000, 25=>22000, 65=>123123];$totals = 0;foreach($uids as $k => $uid){ $amont = $amounts[$uid]; if($amont >= $boundary){ $totals += $amont; }else{ $next = get_next($uids ,$k+1 ,$amont); if($next && is_array($next)){ $curkey = $next[0]; //amouts index 3 $totals+=$next[2]; //再向下获取一层 $nextkey = $curkey+1; if(!isset($uids[$nextkey])){ break; } $nextuid = $uids[$nextkey]; $nextamount = $amounts[$nextuid]; if($nextamount >= $boundary){ $totals+=$nextamount; }else{ $last = get_next($uids ,$nextkey+1 ,$nextamount); if($last && is_array($last)){ $totals+=$last[2]; } } } break; //跳出主循环 }}echo $totals;exit;function get_next($uids ,$start ,$prevamount){ global $amounts ,$boundary; $leaves = array_slice($uids ,$start ,count($uids),true); if($leaves){ foreach($leaves as $k=>$uid){ $amount = $prevamount+$amounts[$uid]; if($amount >= $boundary){ return [$k ,$uid ,$amount]; break; }else{ return get_next($uids ,$k+1 ,$amount); } } } return 0;}

得出$totals=47500
回复内容: 写的不好,求大家优化
一个用户id的数组
$uids = [1,2,3,5,6,8,9,11,13,25,65];

这个数组的每一个键值代表一个uid
金额数组
$amounts = [ 1=>12000, 2=>500, 3=>11000, 5=>1000, 6=>11000, 8=>12000, 9=>12000, 11=>11000, 13=>12000, 25=>22000, 65=>123123];

此数组的键名和$uid数组的键值对应。
循环$uids这个数组,取出对应的金额$amount,
如果这个金额大于等于12000($boundary),就在总金额($totals)加上这个$amount
如果这个金额小于12000,就再向下循环,循环到这几个$amount相加大于等于12000,然后在总金额($totals)加上这几个$amount的和
最多取三层。
最后得出$totals的值。
我现在的代码:
12000, 2=>500, 3=>11000, 5=>1000, 6=>11000, 8=>12000, 9=>12000, 11=>11000, 13=>12000, 25=>22000, 65=>123123];$totals = 0;foreach($uids as $k => $uid){ $amont = $amounts[$uid]; if($amont >= $boundary){ $totals += $amont; }else{ $next = get_next($uids ,$k+1 ,$amont); if($next && is_array($next)){ $curkey = $next[0]; //amouts index 3 $totals+=$next[2]; //再向下获取一层 $nextkey = $curkey+1; if(!isset($uids[$nextkey])){ break; } $nextuid = $uids[$nextkey]; $nextamount = $amounts[$nextuid]; if($nextamount >= $boundary){ $totals+=$nextamount; }else{ $last = get_next($uids ,$nextkey+1 ,$nextamount); if($last && is_array($last)){ $totals+=$last[2]; } } } break; //跳出主循环 }}echo $totals;exit;function get_next($uids ,$start ,$prevamount){ global $amounts ,$boundary; $leaves = array_slice($uids ,$start ,count($uids),true); if($leaves){ foreach($leaves as $k=>$uid){ $amount = $prevamount+$amounts[$uid]; if($amount >= $boundary){ return [$k ,$uid ,$amount]; break; }else{ return get_next($uids ,$k+1 ,$amount); } } } return 0;}

得出$totals=47500
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product