首页 > 解决方案 > 将 x 数量的金币分配给 n 个盗贼

问题描述

假设有 10 个小偷和 100 个金币要分配,分配模式是这样的:

Thief 1 gets 1 coin.
Thief 2 gets 2 coins.
Thief 3 gets 3 coins.... and so on upto 10.

当所有小偷都收到硬币时,总硬币的总和为(1+2+3+... = 55 coins)。现在剩下的硬币是 45。现在,我怎样才能从小偷开始重新分配,但使用最后一个增量值而不是再次从 1 个硬币开始?就像第二轮中的第一个小偷应该得到 11 个硬币而不是 1 个,并且应该一直持续到所有硬币都分配完并且剩下的硬币为 0。如果最后一个小偷必须得到 7 个硬币但可用的硬币是 3 个,那么他应该得到 3 个硬币并且分发应该结束了。

我试过这个...

$thieves = 10;
$goldCoins = 100;

$thiefArr = range(1, $thieves);
$assgnCoins = 0;

foreach($thiefArr as $key => $value){

  for($i=1; $i<=$goldCoins; $i++){

    $assgnCoins = $value; // This assigns first round of coins but how to redistribute it again I have no idea. 

  }
  echo "Thief ".$value." will have ".$assgnCoins." gold coins. <br><br>";
}

标签: phploopsfor-loopforeach

解决方案


我不确定我是否正确理解了您的问题,但这是我的看法,希望这会有所帮助:

<?php
$numt = 10; //Total number of thieves
$totalCoins = 100; //Total coins to redistribute
$data = []; //Data array (will contains the index, which is the "thief" number, and the value of a specific index is the total amount of coins for that specific thief

//Loop init: $coins is the amount of coins given to a thief in a specific distribution round
//Loop condition: We loop until there are still coins to redistribute
//Loop statement: We add 1 coin to each redistribution round
// (I start from 0 and use $coins + 1 because I do modulus on $numt ($coins % $numt) which will give me a number from 0 to $numt - 1 (Hint/fact: array indexes start from 0 and not 1)
for ($coins = 0; $totalCoins > 0; $coins++)
{
    //This will always give me a number between 0 and $numt - 1 (so that we know which thief's turn is)
    //(If this is not clear, you can print out $coins and $thiefIndex then you'll see/understand why I do this)
    $thiefIndex = ($coins % $numt);

    //Because we did not initialize $data values, we check if this specific thief's coins amount has been initialized
    if (!isset($data[$thiefIndex]))
    {
        $data[$thiefIndex] = 0; //Every thief starts with 0 coins
    }

    //Because we started from 0, we need to add 1 (this is the amount of coins that this thief ($thiefIndex) will get in this redistribution round
    $coinsToGive = ($coins + 1);

    //If there's not enough coins left, we just give the total coins remaining
    if ($totalCoins < $coinsToGive)
    {
        $coinsToGive = $totalCoins;
    }

    //Here we sum up all coins that a thief receives in a specific redistribution round
    $data[$thiefIndex] += $coinsToGive;

    //We need to subtract the given coins to the total conins that we redistribute (this makes the loop break when it reaches 0 (see the loop condition part)
    $totalCoins -= $coinsToGive;
}

//Use data/print in your case
foreach ($data as $idx => $tot)
{
    echo "Thief ".($idx + 1)." will have ".$tot." gold coins. <br />";
}

编辑:我想你在我写答案的时候编辑了这个问题。无论如何,如果您想查看每个小偷在每一轮重新分配中获得了多少硬币,您可以在第一个循环中(最后)添加一个回声。

如果您是编程新手,您可能不知道 += 或 -= 的含义:

$foo += $bar;

是相同的:

$foo = $foo + $bar;

这可以应用于每个算术运算符,如 +、-、*、/(甚至其他符号,如 &= 和 |= 等......但这些将是按位运算)

有关运营商的信息可以在 PHP 网站上在线找到。


推荐阅读