均匀分配奖池

时间:2019-02-09 08:57:31

标签: php algorithm math

因此,我看到的最大帮助是来自此线程:Distribute prizes for a tournament system

我已经阅读了其他几本,但是由于它们是一种语言或只是一系列难以理解的字母,它们对我并没有帮助。

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

因此,482.17中通常有15%应该是72.33,这意味着第1位应该获得该值。但是,它返回的值为164.26。尽管该示例声称做到了这一点,但482.17的30%甚至是30%是144.65

1 个答案:

答案 0 :(得分:0)

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