如何解决将 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>";
}
解决方法
我不确定我是否正确理解了您的问题,但这是我的看法,希望对您有所帮助:
<?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 网站上在线找到有关运算符的信息。
,<?php
$thieves = range(1,10);
$availableCoins = 100;
$assignCoins = 0;
while($availableCoins > 0) {
foreach($thieves as $thief) {
// increase the coins to assign
$assignCoins++;
// check if assign coins are greater than the availableCoins
if ($assignCoins > $availableCoins) {
$assignCoins = $availableCoins;
}
echo "Thief ".$thief." will have ".$assignCoins." gold coins. <br><br>";
// substract assignedCoins from availableCoins
$availableCoins -= $assignCoins;
// break the loop if no coins left
if ($availableCoins <= 0) {
break;
}
}
}
,
我现在编辑了@julian S的答案,给出了作业应该给出的结果:
我还添加了一些更多的 HTMl 输入来演示数组的结构、正在运行的操作以及以可读形式生成的数组。
简而言之:
这里的范围是错误的。
因为它将用 1,2,3,4,5,6,7,8,9 填充一个数组,这已经是一个数值数组,所以成员 1 的值为 1 ??无用。不!
因此,我们将 10thives weach 的数组创建为初始值 = - 因为小偷最初拥有 = money。
我们打印这个数组并添加 pre 使它看起来好一点:-)
然后我们有两个循环 - 外部 while 循环具有查看是否还有钱的功能。如果还有moey,那么再做一轮。 这是原始海报缺少的逻辑,因为他确实明白他需要为 10 个小偷设置一个循环。
然后是内循环
只需从 1 数到小偷的数量。所以每个小偷都会得到它的份额。 $a += $b 是 $a=$a+$b
的简写形式将当前币添加到当前数组成员
如果在 10 个小偷结束之前 monex 就消失了,请添加一个中断以离开 for 循环。
最后只打印盗贼财富的数组。
0-9 作为数组以 0 开头。
<?php
$thieves = 10;
$availableCoins = 100;
$assignCoins = 0;
$array_thieves= array_fill(0,10,0);
echo "<h2>Initial array with wealth of each thief</h2> ";
echo "<pre>";
print_r($array_thieves)."<br>";
echo"</pre>";
while($availableCoins > 0) { //inerate as long as there are coins - outer loop
for($i=0; $i<$thieves; $i++) { //execute loop once per thieve so 10 x per round -inner loop
// increase the coins to assign
$assignCoins++; //we have started with 0 and each time we add 1
// check if assign coins are greater than the availableCoins
//if the number of available coins is smaller then just assign all available coins
if ($assignCoins > $availableCoins) {
$assignCoins = $availableCoins;
}
$array_thieves[$i] += $assignCoins; //add the coins to the thied (= array member) with the current number $i [0-9]
echo "Thief ". $i ." gets now additional ".$assignCoins." gold coins. He has now a total of: ". $array_thieves[$i] ." <br>";
// substract assignedCoins from availableCoins
$availableCoins -= $assignCoins;
// break the FOR loop if no coins left - otherwise the inner loop will continue
if ($availableCoins <= 0) {
break;
}
}
}
echo"<h2>Resulting array with all thieves and their wealth</h2>";
echo"<pre>";
print_r($array_thieves);
echo"</pre>";
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。