gpt4 book ai didi

php - php中的循环算法等于主场分布

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:10:39 27 4
gpt4 key购买 nike

我一直在寻找这个解决方案,但文档很少,但无法正确获取。请看一看。我尝试实现循环算法来创建比赛时间表,但堆叠在主客场分布上。

$allTeam=[3,10,8,7];
$leng=sizeof($allTeam);
$lastHalf=$leng-1;
for ($t=0; $t <$leng-1 ; $t++) {
for ($i=0; $i < $leng/2; $i++) {
if($t % 2== 0){
\Log::info('Home Team => '.$allTeam[$i].' vs '. $allTeam[$lastHalf-$i].'<=Away Team');
}else{
\Log::info('Away Team =>'.$allTeam[$i].' vs '. $allTeam[$lastHalf-$i].'<= Home Team');
}

}

/*now rotate the array. For this first insert the last item into postion 1*/
array_splice( $allTeam, 1, 0, $allTeam[$leng-1]);
/*now pop up the last element*/
array_pop($allTeam);
}

这是结果

Home Team => 3 vs 7<=Away Team  
Home Team => 10 vs 8<=Away Team

Away Team =>3 vs 8<= Home Team
Away Team =>7 vs 10<= Home Team

Home Team => 3 vs 10<=Away Team
Home Team => 8 vs 7<=Away Team

您可以看到它分布不均。我如何确定如果一支球队在第一轮作为主队比赛,那么这支球队应该在下一轮比赛中客场比赛?同样,如果第一轮客场然后下一轮回家?

谢谢。

最佳答案

这比你现在做的更容易,如果你只创建每个排列,那么每支球队将在球队之间进行主客场比赛的次数相等,所以如果有 4 支球队,你将有 3 主场 3 客场,生成会像这样完成:

<?php
$allTeam = [3, 10, 8, 7];

$result = [];
foreach ($allTeam as $home) {
foreach ($allTeam as $away) {
if ($home === $away) {
continue;
}
$result[] = 'Home Team => '.$home.' vs '.$away.' <= Away Team';
}
}

print_r($result);

https://3v4l.org/EAl8G

结果:

Array
(
[0] => Home Team => 3 vs 10 <= Away Team
[1] => Home Team => 3 vs 8 <= Away Team
[2] => Home Team => 3 vs 7 <= Away Team
[3] => Home Team => 10 vs 3 <= Away Team
[4] => Home Team => 10 vs 8 <= Away Team
[5] => Home Team => 10 vs 7 <= Away Team
[6] => Home Team => 8 vs 3 <= Away Team
[7] => Home Team => 8 vs 10 <= Away Team
[8] => Home Team => 8 vs 7 <= Away Team
[9] => Home Team => 7 vs 3 <= Away Team
[10] => Home Team => 7 vs 10 <= Away Team
[11] => Home Team => 7 vs 8 <= Away Team
)

然后您将有另一个循环,以某种方式对它们进行排序,使其看起来像 home -> away -> home -> away 场景。

关于php - php中的循环算法等于主场分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49837459/

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