我需要帮助,例如,我在那里得到了包含 4 的团队名称数组。
array('logiX.L4d22','Lust','Marat and Friends','Pandas of Belgium');
我想制作一个三维数组,其中第一个是一轮,第二个是一场比赛,第三个是互相比赛的球队,总是有 2 支球队。
逻辑必须使所有球队必须与所有其他球队一起比赛,并且在一轮中,任何球队只能参加一场比赛,所以如果我们有 5 支球队,那么在某一轮中,一支球队必须等待下一轮。
它必须产生这样的结果:
array 0 => array 0 => array 0 => string 'logiX.L4D2' (length=10) 1 => string 'Lust' (length=4) 1 => array 0 => string 'Marat and Friends' (length=17) 1 => string 'Pandas of Belgium' (length=17) 1 => array 0 => array 0 => string 'logiX.L4D2' (length=10) 1 => string 'Marat and Friends' (length=17) 1 => array 0 => string 'Lust' (length=4) 1 => string 'Pandas of Belgium' (length=17) 2 => array 0 => array 0 => string 'logiX.L4D2' (length=10) 1 => string 'Pandas of Belgium' (length=17) 1 => array 0 => string 'Lust' (length=4) 1 => string 'Marat and Friends' (length=17)
必须与 2,3,5 ...10 ...12 个团队合作。
我希望你能帮助我,我已经花了 1 天半的时间了。
最佳答案
对 循环算法 PHP 进行一些谷歌搜索,得出以下结果:
http://speedtech.it/blog/2009/03/15/round-robin-algorithm-php/
http://www.phpbuilder.com/board/showthread.php?t=10300945
希望您能找到您想要的东西。
编辑
按照round-robin algorithm described on Wikipedia添加我对此的尝试.
如果队伍数量为奇数,则会在数组中添加一个队伍(空值),这样您就可以检索每轮的“等待队伍”。
<?php
$teams = range('a', 'g');
function make_rounds($teams)
{
$nb_teams = count($teams);
if ($nb_teams % 2 != 0)
{
$teams[] = null;
$nb_teams++;
}
$nb_rounds = $nb_teams - 1;
$nb_matches = $nb_teams / 2;
$rounds = array();
for($round_index = 0; $round_index < $nb_rounds; $round_index++)
{
$matches = array();
for($match_index = 0; $match_index < $nb_matches; $match_index++)
{
if ($match_index == 0)
$first_team = $teams[0];
else
$first_team = $teams[(($nb_teams-2) + $match_index - $round_index) % ($nb_teams-1) + 1];
$second_team = $teams[(($nb_teams*2) - $match_index - $round_index - 3) % ($nb_teams-1) + 1];
$matches[] = array($first_team, $second_team);
}
$rounds[] = $matches;
}
return $rounds;
}
print_r(make_rounds($teams));
关于php - 需要逻辑帮助,制作三维数组(PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5485728/