首页 > 解决方案 > 平均分配奖池

问题描述

所以我看到的最大帮助来自这个线程:为锦标赛系统分发奖品

我读过一些其他的,但它们对我没有帮助,因为它是一种语言,或者只是一系列稍微难以理解的字母。

例如,我的奖金价值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

标签: phpalgorithmmath

解决方案


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";
}
?>

推荐阅读