gpt4 book ai didi

php - 根据位置将 'points' 分配给数组,如何处理平局?

转载 作者:可可西里 更新时间:2023-11-01 13:30:37 25 4
gpt4 key购买 nike

我有一个数组,里面装满了带有名称的数组,然后是总金额。然后根据他们的总排名为这个数组分配一个“总分”。每升一级积分减少 2,但我的问题是如何处理平局?理想情况下,我会查看有多少平局,将这些排名的总分相加,然后除以平局数,但我不知道如何真正做到这一点

这是我的数组排序:

function grossSort($gross, $compare) {
if($gross['gross'] > $compare['gross'])
return -1; // move up
else if($gross['gross'] < $compare['gross'])
return 1; // move down
else
return 0; // do nothing
}

将我的数组变成这样:

Array
(
[0] => Array
(
[instr] => lee
[gross] => 2094
)

[1] => Array
(
[instr] => steve
[gross] => 1334
)

[2] => Array
(
[instr] => nick
[gross] => 0
)

[3] => Array
(
[instr] => amber
[gross] => 0
)

[4] => Array
(
[instr] => lindsey
[gross] => 0
)

)

下面是我现在分配点数的方法:

$maxpoints = 40;            
for($i = 0; $i < count($trainergross); $i++){
$trainergross[$i]['points'] += $maxpoints;
$maxpoints -=2;
}

现在我的数组看起来像这样:

Array
(
[0] => Array
(
[instr] => lee
[gross] => 2094
[points] => 40
)

[1] => Array
(
[instr] => steve
[gross] => 1334
[points] => 38
)

[2] => Array
(
[instr] => nick
[gross] => 0
[points] => 36
)

[3] => Array
(
[instr] => amber
[gross] => 0
[points] => 34
)

[4] => Array
(
[instr] => lindsey
[gross] => 0
[points] => 32
)

)

这是我的问题,有 4 个人“并列”但分数越来越低,我不知道如何以我的代码设置的当前方式解决这个问题。有人有任何指导吗?

最佳答案

您可以在迭代时存储最后一个已知值,并且仅在值更改时减少点数。数组已排序,因此相等的元素将相邻。

/**
* award average of points to a number of elements.
* @param arrayref $trainergross - reference to the original array
* @param int $award - total points to award
* @param int $start_index - which element to start with?
* @param int $count - how many elements should be awarded points?
* @return void
*/
function award_points(&$trainergross, $award, $start_index, $count=1)
{
if(!$count || $award<1) return; // if noone is waiting for the award, OR there is no award, bail out

// $awards holds total award, and we need to
$avgPoints = $award / $count;

// award points to $count previous elements
for($j = $start_index; $j < $start_index + $count; ++$j)
{
// in case points key was not initialized, php will issue a notice.
if(!isset($trainergross[$j]['points'])) $trainergross[$j]['points'] = 0;

$trainergross[$j]['points'] += $avgPoints;
}
}

/**
* This is increased on purpose
* the first check will be <some number> vs null, which will result in -=2.
* this is to eliminate additional !== null test inside the loop.
*/
$maxpoints = 42;

// This will be used to store number of consecutive elements
$count = 0;

// This will be used to store total points to award
$award = 0;

// Here we will store last gross value
$last = null;

for($i = 0; $i < count($trainergross); $i++)
{
$maxpoints -= 2;

// if gross has not changed, just count consecutive elements and continue
if($last == $trainergross[$i]['gross'])
{
$award += $maxpoints;
$count++;
continue;
}

// store gross value
$last = $trainergross[$i]['gross'];

// now really distribute the points we've gathered in award by now
award_points($trainergross, $award, $i - $count, $count);

// reset count back to 1 (there is only 1 "consecutive" element now)
$count = 1;

// next element will get this award
$award = $maxpoints;
}

// now we have to award points again
// the loop ended, but $count of elements are still awaiting points...
award_points($trainergross, $award, $i - $count, $count);

关于php - 根据位置将 'points' 分配给数组,如何处理平局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18192824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com