php - 平均分配奖池
问题描述
所以我看到的最大帮助来自这个线程:为锦标赛系统分发奖品。
我读过一些其他的,但它们对我没有帮助,因为它是一种语言,或者只是一系列稍微难以理解的字母。
例如,我的奖金价值482.17
分布在 45 位参与者中,我想做的是以 15% 的间隔分配这个价值。
从上一个线程我已经尽力翻译这个
int i;
int prizes[21];
int money=1000;
for(i = 1; i <= 20; i++){
prizes[i] = (float) (15+(20-i)) / 100 * money;
money -= prizes[i];
fprintf(g,"%d) %d\n",i,prizes[i]);
}
到 PHP,这就是我下面的内容。
$Points = 482.17
$countPlayers = 45;
for ($i = 0; $i < $countPlayers; $i++) {
/* Calculate Points */
$reward = (float) (15 + ($countPlayers - $i)) / 100 * $Points;
$Points -= $reward;
echo $getPlayers[i] . " - " . $Points;
}
因此,通常 15%482.17
应该72.33
意味着第一名应该获得该价值。但相反,它返回一个值164.26
。尽管该示例声称执行了 30%,但即使是 482.17 的 30%144.65
解决方案
I think this does what you're asking for.
<?php
$countPlayers =45;
$prizes[$countPlayers];
$money=482.17;
for($i = 1; $i <= $countPlayers; $i++){
// Take 15% of the remaining pot each time
$prizes[$i] = ($money * 15.0)/100.0;
$money -= $prizes[$i];
echo $i.")".number_format($prizes[$i],2,'.','')."\n";
}
?>
推荐阅读
- jenkins - 如何在 Jenkins 中制作自定义报告?
- sql - 只有在没有其他记录时才保留空记录
- jsf - 如何从支持 bean 调用 javascript 函数并让被调用函数返回响应?
- c# - 使用手动添加的 settings.json 文件到 dotnet 核心控制台应用程序时,Configuration.GetSection("SectionName") 始终为空
- android - 如何在firebase db android中从多对多关系获取完整数据?
- flutter - 如何在列表视图中显示列表视图
- javascript - Javascript中单个变量的长编号列表?
- python - 为什么命令及其参数必须在 subprocess.Popen 的列表中?
- react-native - CreateMaterialTopTabNavigator 如何添加第三个选项卡
- javascript - 从每个语句中迭代超过 4 个随机响应