【发布时间】:2019-03-22 19:43:18
【问题描述】:
我正在尝试根据分数查找玩家的记录。如果两名球员得分相同,那么比赛次数少的球员排名靠前。如果两名球员的得分和比赛次数并列,那么在球员名单中排名第一的球员排名更高。
这是迄今为止我尝试过的代码。
http://sandbox.onlinephpfunctions.com/code/ca5f93990e1cb6a69bbbd734afde32f1ffa51522
但我得到的答案不正确。
<?php
class LeagueTable
{
public function __construct($players)
{
$this->standings = array();
foreach($players as $index => $p)
{
$this->standings[$p] = array
(
'index' => $index,
'games_played' => 0,
'score' => 0
);
}
}
public function recordResult($player, $score)
{
$this->standings[$player]['games_played']++;
$this->standings[$player]['score'] += $score;
}
public function playerRank($rank)
{
$ranks = $this->standings;
uasort($ranks, function ($a, $b) {
if ($a['score'] >= $b['score']) {
if ($a['score'] == $b['score']) {
if ($a['games_played'] >= $b['games_played']) {
if ($a['games_played'] == $b['games_played']) {
return $a['index'] > $b['index'];
} else {
return true;
}
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
});
print_r($this->standings);
}
}
$table = new LeagueTable(array('Mike', 'Chris', 'Arnold'));
$table->recordResult('Mike', 2);
$table->recordResult('Mike', 3);
$table->recordResult('Arnold', 5);
$table->recordResult('Chris', 5);
echo $table->playerRank(1);
结果
I am getting Array
(
[Mike] => Array
(
[index] => 0
[games_played] => 2
[score] => 5
)
[Chris] => Array
(
[index] => 1
[games_played] => 1
[score] => 5
)
[Arnold] => Array
(
[index] => 2
[games_played] => 1
[score] => 5
)
) BUT my test says Example case: Wrong answer
Players have different scores: Wrong answer
Players tied by score: Wrong answer
Players tied by games played: Wrong answer
【问题讨论】:
-
你得到什么答案?请注明。
-
@Wreigh 我已经更新了问题
标签: php