我需要帮助,例如,我在那里得到了4个用于团队名称的数组。
array('logiX.L4d22','Lust','Marat and Friends','Pandas of Belgium');
我想制作三维数组,其中第一个是圆形,第二个是匹配,第三个是与彼此一起玩的团队,总会有2个团队。
逻辑必须使所有球队必须与所有其他球队一起进行,并且在一轮比赛中,任何一支球队只能进行一场比赛,所以如果我们有5支球队,那么在一轮比赛中,一支球队必须等到下一轮。 / p>
它必须产生这样的东西:
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队合作。
我希望你能帮助我,我已经花了一天半的时间。
答案 0 :(得分:2)
关于循环算法PHP 的一些Google搜索提供了以下内容:
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));
答案 1 :(得分:1)
我的解决方案版本。我会称之为蛮力。但是,它以某种方式工作。 (或者它看起来像那样。)
<?php
$a = array('a','b','c','d','e','f','g');
function do_array($a)
{
$lim = sizeof($a) - 1;
# Create an array of all matches to play.
$cross = array(array());
foreach (range(0,$lim) as $k_row):
foreach (range(0,$lim) as $k_col):
if ($k_row >= $k_col):
$toput = false;
else:
$toput = array($a[$k_row],$a[$k_col]);
endif;
$cross[$k_row][$k_col] = $toput;
endforeach;
endforeach;
$ret = array();
foreach (range(0,$lim) as $k_round):
$round = array();
# $tmp array holds all possible matches
# to play in current round.
$tmp = $cross;
$i = 0;
foreach (range(0,$lim) as $k_row):
foreach (range(0,$lim) as $k_col):
if ($math = $tmp[$k_row][$k_col]):
$cross[$k_row][$k_col] = false;
# These matches are not possible
# in the current round.
foreach (range(0,$lim) as $k):
$tmp[$k][$k_col] = false;
$tmp[$k][$k_row] = false;
$tmp[$k_col][$k] = false;
$tmp[$k_row][$k] = false;
endforeach;
$round[] = $math;
endif;
endforeach;
endforeach;
if ($round):
$ret[] = $round;
endif;
endforeach;
return $ret;
}
print_r (do_array($a));
?>