2016-02-17 102 views
0

我想建立一個足球比賽,我必須在球隊之間生成所有回合。 然後,我有這樣的怎樣才能生成一輪排列

$array = array(1,2,3,4,5,6,7,8); 

怎樣才能不用重複元素之間的所有置換的數組。

array(1,2) 
array(3,4) 
array(5,6) 
array(7,8) 

下一迭代

array(1,3) 
array(2,4) 
array(5,7) 
array(6,8) 

隨着正常排列接收重複這樣

(1 2) (3 4) (5 6) (7 8) 
(2 1) (3 4) (5 6) (7 8) 

3和4已經在上一輪。同樣的5到6和7到8.

+0

你需要根據您的條件創建數組排列的算法? –

+2

8支隊伍(隊伍#0....'隊伍#7'),7天('#1'..'day#7')。如果'A xor B = D',則#隊'#'在#日'#隊'上進行比賽# –

+0

@EgorSkriptunoff這真是太棒了。做一個答案。 –

回答

1

爲了完整起見(上SO),這裏是從another answer張貼(感謝M69的評論以上)的代碼:

/****************************************************************************** 
* Round Robin Pairing Generator 
* Author: Eugene Wee 
* Date: 23 May 2005 
* Last updated: 13 May 2007 
* Based on an algorithm by Tibor Simko. 
* 
* Copyright (c) 2005, 2007 Eugene Wee 
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy 
* of this software and associated documentation files (the "Software"), to deal 
* in the Software without restriction, including without limitation the rights 
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is 
* furnished to do so, subject to the following conditions: 
* 
* The above copyright notice and this permission notice shall be included in 
* all copies or substantial portions of the Software. 
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
* THE SOFTWARE. 
******************************************************************************/ 

function generateRoundRobinPairings($num_players) { 
    // Do we have a positive number of players? If not, default to 4. 
    $num_players = ($num_players > 0) ? (int)$num_players : 4; 

    // If necessary, round up number of players to nearest even number. 
    $num_players += $num_players % 2; 

    // Format for pretty alignment of pairings across rounds. 
    $format = "%0" . ceil(log10($num_players)) . "d"; 
    $pairing = "$format-$format "; 

    // Set the return value 
    $ret = $num_players . " Player Round Robin:\n-----------------------"; 

    // Generate the pairings for each round. 
    for ($round = 1; $round < $num_players; $round++) { 
     $ret .= sprintf("\nRound #$format : ", $round); 
     $players_done = array(); 

     // Pair each player except the last. 
     for ($player = 1; $player < $num_players; $player++) { 
      if (!in_array($player, $players_done)) { 
       // Select opponent. 
       $opponent = $round - $player; 
       $opponent += ($opponent < 0) ? $num_players : 1; 

       // Ensure opponent is not the current player. 
       if ($opponent != $player) { 
        // Choose colours. 
        if (($player + $opponent) % 2 == 0 xor $player < $opponent) { 
         // Player plays white. 
         $ret .= sprintf($pairing, $player, $opponent); 
        } else { 
         // Player plays black. 
         $ret .= sprintf($pairing, $opponent, $player); 
        } 

        // This pair of players are done for this round. 
        $players_done[] = $player; 
        $players_done[] = $opponent; 
       } 
      } 
     } 

     // Pair the last player. 
     if ($round % 2 == 0) { 
      $opponent = ($round + $num_players)/2; 
      // Last player plays white. 
      $ret .= sprintf($pairing, $num_players, $opponent); 
     } else { 
      $opponent = ($round + 1)/2; 
      // Last player plays black. 
      $ret .= sprintf($pairing, $opponent, $num_players); 
     } 
    } 

    return $ret; 
} 
-1

我使用array_filter來清除重複和團隊會自己玩的情況。

<?php 
$teams = range(1,8); 

$match_permutations = array(); 

foreach($teams as $team1) { 
    foreach($teams as $team2) { 
     $match_permutations[] = array($team1, $team2); 
    } 
} 

//var_dump($match_permutations); 

$match_combinations = array_filter($match_permutations, 
    function($item) { 
     if($item[0] < $item[1]) return true; 
    } 
); 

var_dump($match_combinations); 
+0

參見:http://stackoverflow.com/a/35453060/ 3392762爲什麼這個工程。 – Progrock

+0

敢問一問,爲什麼要投票? – Progrock

+0

這不是我...... –

-1

如果我們有四支球隊,我們可以寫出所有可能的匹配排列是這樣的:

(1, 1) (1, 2) (1, 3) (1, 4) 
(2, 1) (2, 2) (2, 3) (2, 4) 
(3, 1) (3, 2) (3, 3) (3, 4) 
(4, 1) (4, 2) (4, 3) (4, 4) 

你可以看到在對角線鏡像重複。

我用下面的代碼輸出上面:

for($i=1; $i<5; $i++) { 
    for($j=1; $j<5; $j++) { 
     echo "($i, $j) "; 
    } 
    echo "\n"; 
} 

如果我們只關心楹聯其中$ i小於附加$ J。或者,在$ i大於$ j的情況下,我們刪除重複項和對角線本身(球隊自己的位置)。

function team_combinations($no_teams) { 
    $combinations = array(); 
    for($i=1; $i<=$no_teams; $i++) { 
     for($j=1; $j<=$no_teams; $j++) { 
      if($i < $j) 
       $combinations[] = array($i, $j); 
     } 
    } 

    return $combinations; 
} 

var_export(team_combinations(4)); 

輸出:

array (
    0 => 
    array (
    0 => 1, 
    1 => 2, 
), 
    1 => 
    array (
    0 => 1, 
    1 => 3, 
), 
    2 => 
    array (
    0 => 1, 
    1 => 4, 
), 
    3 => 
    array (
    0 => 2, 
    1 => 3, 
), 
    4 => 
    array (
    0 => 2, 
    1 => 4, 
), 
    5 => 
    array (
    0 => 3, 
    1 => 4, 
), 
)