循环算法在php

时间:2018-04-15 00:34:22

标签: php algorithm round-robin

我一直在寻找这个解决方案,而且文档很少,但无法正确使用它们。请看一看。我尝试实现循环算法来创建匹配计划,但堆栈在主页上分发。

$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  

你可以看到它的分布不均匀。 我怎样才能确保如果一支球队在第一轮中作为主队出战,那么这支球队应该在下一轮比赛中打败?同样,如果在第一轮离开然后在下一轮回家?

谢谢。

2 个答案:

答案 0 :(得分:1)

这比你正在做的更容易,如果你只是创造每个排列,那么每个团队将在团队之间玩回家和相同数量的时间,所以如果有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
)

然后你会有另一个循环,以一种让它看起来像家的方式对它们进行排序 - &gt;离开 - &gt; home - &gt;离开场景。

答案 1 :(得分:0)

我通过一些修改解决了它。这是我的最终代码。它可能会帮助其他人

$allTeam=[1,2,3,4];
    /*now make the draw and insert it in the table*/
    $leng=sizeof($allTeam);
    $firstHalf=0;
    $lastHalf=$leng-1;

    $isHome=true;
    for ($t=0; $t <$leng-1 ; $t++) { 
        for ($i=0; $i < $leng/2; $i++) {
            if($i==0){
                if($isHome){
                    \Log::info('Home team=> '.$allTeam[$i].' vs Away team=> '.$allTeam[$lastHalf-$i]);
                    $isHome=false;
                }else{
                    \Log::info('Home team=> '.$allTeam[$lastHalf-$i].' vs Away team=> '.$allTeam[$i]);
                    $isHome=true;
                }
            }else{
                if($i%2==0){
                /*make first half home team*/
                \Log::info('Home team=> '.$allTeam[$i].' vs Away team=> '.$allTeam[$lastHalf-$i]);

                }else{
                    \Log::info('Home team=> '.$allTeam[$lastHalf-$i].' vs Away team=> '.$allTeam[$i]);  
                }
            }


        }
        /*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);

    }