寫的不好,請大家優化
一個用戶ID的陣列
<code>$uids = [1,2,3,5,6,8,9,11,13,25,65];</code>
這個陣列的每一個鍵值代表一個UID
金額數組
<code>$amounts = [ 1=>12000, 2=>500, 3=>11000, 5=>1000, 6=>11000, 8=>12000, 9=>12000, 11=>11000, 13=>12000, 25=>22000, 65=>123123 ];</code>
此數組的鍵名和$uid數組的鍵值對應。
循環$uids這個數組,取出對應的金額$amount,
如果這個金額大於等於12000($boundary),就在總金額($totals)加上這個$amount
如果這個金額小於12000,就再向下循環,循環到這幾個$amount相加大於等於12000,然後在總金額($totals)加上這幾個$amount的和
最多取三層。
最後得出$totals的值。
我現在的程式碼:
<code><?php $boundary = 12000; $uids= [1,2,3,5,6,8,9,11,13,25,65]; $amounts = [ 1=>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; }</code>
得出$totals=47500
寫的不好,請大家優化
一個用戶ID的陣列
<code>$uids = [1,2,3,5,6,8,9,11,13,25,65];</code>
這個陣列的每一個鍵值代表一個UID
金額數組
<code>$amounts = [ 1=>12000, 2=>500, 3=>11000, 5=>1000, 6=>11000, 8=>12000, 9=>12000, 11=>11000, 13=>12000, 25=>22000, 65=>123123 ];</code>
此數組的鍵名和$uid數組的鍵值對應。
循環$uids這個數組,取出對應的金額$amount,
如果這個金額大於等於12000($boundary),就在總金額($totals)加上這個$amount
如果這個金額小於12000,就再向下循環,循環到這幾個$amount相加大於等於12000,然後在總金額($totals)加上這幾個$amount的和
最多取三層。
最後得出$totals的值。
我現在的程式碼:
<code><?php $boundary = 12000; $uids= [1,2,3,5,6,8,9,11,13,25,65]; $amounts = [ 1=>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; }</code>
得出$totals=47500