2011-03-30 76 views
2

我需要幫助,例如,我有4個隊列名的數組。需要邏輯幫助,製作三維數組(PHP)

 
array('logiX.L4d22','Lust','Marat and Friends','Pandas of Belgium'); 

我想使3維數組,其中第一個是圓的,第二是比賽,第三個是誰的球隊與海誓山盟玩,始終存在一些2隊。

邏輯必須讓所有的球隊必須與所有其他球員一起打球,並且在一輪中,任何球隊只會打一場比賽,所以如果我們有5支球隊,那麼在某輪比賽中,一支球隊必須等待下一輪。

它必須產生這樣的:

 
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支球隊。

我希望你能幫助我,我已經花了1天半的時間。

+0

真的不清楚。你到底在找什麼?一個數組結構的建議?一個PHP腳本解決方案? – 2011-03-30 11:36:22

+0

我需要PHP腳本解決方案..對於這個不清楚 – Kuido 2011-03-30 11:44:17

+0

http://stackoverflow.com/questions/658727/how-can-i-generate-a-round-robin-tournament-in-php-and -mysql – 2011-03-30 11:51:42

回答

2

循環算法PHP一些谷歌搜索提供了以下:

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)); 
+0

是的..感謝很多 – Kuido 2011-03-30 12:42:32

+0

已更新的回答。我希望你能接受它:) – 2011-03-30 18:58:56

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

?> 
+0

小細節:我建議在這裏使用'for(...)'而不是'foreach(range(...))'。看到這個評論:http://www.php.net/manual/fr/function.range.php#85331 – 2011-03-30 19:01:04

+0

我知道它不是那麼快。不過我認爲它更具可讀性。 – Michas 2011-03-30 19:41:03