calculateNumberCredits(25000);
function calculateNumberCredits($experience) {
# The credits we have
$credits = array(
'a' => '10000',
'b' => '2000',
'c' => '1000',
);
# Keep track of the amount needed per credit
$timesCreditNeeded = array();
# Start calculating the amount per credit we need
foreach($credits as $creditID => $creditAmount) {
# 1) Calculate the number of times the amount fits within the amount of experience
$times = floor($experience / $creditAmount);
# 2) Calculate the remainder of the above division en cache is for the next calculation
$experience = $experience % $creditAmount;
# 3) Cache the number of times the credit fits within the experience
$timesCreditNeeded[$creditID] = $times;
}
echo '<pre>';
print_r($timesCreditNeeded);
return $timesCreditNeeded;
}
// Will return Array ( [a] => 2 [b] => 2 [c] => 1 )
我循环浏览您系统中的积分。在这个例子中,信用是从高到低的顺序。如果不是这种情况,您应该订购它们以获得所需的结果。
1)对于每个信用,我尝试找到信用适合特定用户体验的最大次数。因为 floatnumber 没有意义,所以我们 floor() 是除法的结果。
2)在我找到信用拟合的次数后,我计算下一次迭代的余数(下一个信用)。您可以通过计算模数找到余数。
3)最后但并非最不重要的一点是,我缓存了信用适合的次数。
希望这可以帮助!