我想根据他们有多少朋友给予我的玩家奖金。
我有断点(例如0,1,5,10,25)
对于0个朋友,他获得0奖金。
对于1位朋友,他获得1000,5或者超过2000等...
我现在做的是:
public function getFriendsBonusByFriendsAmount($amount)
{
switch (true) {
case ($amount < 1):
return 0;
case ($amount < 5):
return 1000;
case ($amount < 10):
return 2000;
case ($amount < 25):
return 3000;
case ($amount >= 25):
return 5000;
}
}
我正在寻找一种不同的方法来找到没有foreach / switch的奖金
或许想到我可以玩的数组或数组?
$bonusBreakpoints = [
0 => 0,
1 => 1000,
5 => 2000,
10 => 3000,
25 => 5000
]
或者两个具有相应索引的数组?
我想到了一种方法,但这是一种记忆浪费:
$bonusPerFriends = [
0 => 0,
1 => 1000,
2 => 1000,
3 => 1000,
4 => 1000,
5 => 2000,
6 => 2000,
...
25 => 5000
]
我宁愿不这样使用。
答案 0 :(得分:0)
@ andrey-mischenko的答案在技术上是正确的,但没有解决问题没有foreach ,正如你在问题中所说。(编辑:答案被删除)试试这个:
$bonusBreakpoints = [
0 => 0,
1 => 1000,
5 => 2000,
10 => 3000,
25 => 5000
];
$justTheKeys = array_keys($bonusBreakpoints);
public function getFriendsBonusByFriendsAmount($amount)
{
$bonus = array_reduce($justTheKeys, function($carryOver, $item) use ($amount)
{
if ($amount >= $item) return $bonusBreakpoints($item);
return $carryOver;
}
return $bonus;
}
(我知道这不是array_reduce
原本打算用的。我把这个问题理解为一个思维游戏。就像“找到创造性的方法解决这个问题,除了明显的问题,比如循环或者开关。“如果我必须为此工作编码,我也可能使用循环。:))
答案 1 :(得分:0)
好吧,有时foreach/switch
将是最佳解决方案:)
/**
* Calculates bonus based on how many
* friends the player have from predefined breakpoints
**/
function getBonus($friends) {
$bonuses = [0, 1000, 2000, 3000, 5000];
$stops = [[PHP_INT_MIN, 0], [1, 4], [5, 14], [15, 24], [25, PHP_INT_MAX]];
// replace the stops by bonus if match, otherwise return empty - O(n)
$bonus = array_map(function ($stop, $bonus) use ($friends) {
if ($friends >= $stop[0] && $friends <= $stop[1]) {
return $bonus;
}
}, $stops, $bonuses);
// clean up the array from empty values - O(n)
$bonus = array_filter($bonus , 'is_numeric');
// from array(3 => 3000) to 3000 - O(1)
return array_pop($bonus);
}
<强>结果:强>
echo getBonus(0).PHP_EOL; // 0
echo getBonus(4).PHP_EOL; // 1000
echo getBonus(12).PHP_EOL; // 2000
echo getBonus(20).PHP_EOL; // 3000
echo getBonus(39).PHP_EOL; // 5000
P.S。 $bonuses
和$stops
此处必须等长。
答案 2 :(得分:0)
在阅读答案和更多研究后,我得出结论,二元搜索是我的最佳选择。
数据应该看起来像这样:
$bonuses = [
[ 'min' => 0, 'max' => 0, 'amount' => 0 ]
[ 'min' => 1, 'max' => 4, 'amount' => 1000 ]
[ 'min' => 5, 'max' => 14, 'amount' => 2000 ]
...
[ 'min' => 25, 'max' => PHP_INT_MAX, 'amount' => 5000 ]
]
你从count($bonuses)/2
开始,从那里检查你是否低于分钟,中途是如此,否则检查你是否高于最大值,并在那里走了一半。否则......只要你在正确的范围之间退还红利。
由于我的大多数用户没有朋友或超过25个,我可能会先查看第一个和最后一个单元格。