Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中的轮循算法_Php_Algorithm_Round Robin - Fatal编程技术网

php中的轮循算法

php中的轮循算法,php,algorithm,round-robin,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){

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

$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);
 }
$allTeam=[3,10,8,7];
$leng=sizeof($allTeam);
$lastHalf=$leng-1;

对于($t=0;$t 3 vs 7 10 vs 83 vs 87 vs 10 3 vs 10 8 vs 7这比你所做的更容易,如果你只是创建每个排列,那么每个队在队与队之间主客场比赛的次数相等,因此如果有4个队,你将有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);

这比你所做的更容易,如果你只创建每个排列,那么每个队在队与队之间的主客场比赛次数相等,因此如果有4个队,你将有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);

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

$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);

    }
$allTeam=[1,2,3,4];
/*现在绘制并将其插入表中*/
$leng=sizeof($allTeam);
$firstHalf=0;
$lastHalf=$leng-1;
$isHome=true;

对于($t=0;$t我做了一些修改,解决了它。这是我的最终代码。它可能会帮助其他人

$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);

    }
$allTeam=[1,2,3,4];
/*现在绘制并将其插入表中*/
$leng=sizeof($allTeam);
$firstHalf=0;
$lastHalf=$leng-1;
$isHome=true;

对于($t=0;$t,将$leng减去1两次,而不是将数组一分为二(对于四支球队,结果是一样的,但不适用于更多的球队。只需将一半球队指定为A,另一半为B,A在奇数迭代中在主场比赛,B在偶数迭代中比赛。然后,当你耗尽所有比赛后,对数组进行排列。我对许多球队进行了测试,很多次,该程序在排班方面都很好,但主客场是p问题。你能详细说明一下你给出的主客场解决方案吗?没有得到你的解决方案。如何指定一半的球队为a,一半为B,因为在一轮之后,阵型是旋转的,一半相互混合。你从$leng中减去一两次,而不是将阵型分成两半(对于四支球队,结果是一样的,但不适用于更多的球队。只需将一半球队指定为A,另一半为B,A在奇数迭代中在主场比赛,B在偶数迭代中比赛。然后,当你耗尽所有比赛后,对数组进行排列。我对许多球队进行了测试,很多次,该程序在排班方面都很好,但主客场是p问题。你能详细说明一下你给出的主客场解决方案吗?没有得到你的解决方案。如何指定一半球队为a,一半球队为B,因为在一轮之后阵列会旋转,一半会相互混合。每支球队可以打n-1场比赛,所以如果有4支球队,那么每支球队将打3次。你的程序看起来不错,但确实如此esn不适用于此目标。每支球队可以打n-1场比赛,因此如果有4支球队,则每支球队将打3次。您的计划看起来不错,但它不适用于此目标。