2011-01-11 93 views
0

我正在为我的公司开发一个汽车共享程序的应用程序出现问题(该过程有点复杂)。我想要做的是follwoing:使用3个阵列创建匹配

我有3支球队的3人,每队有例如独特的会员ID:

集团1 =(1,2,3,4)

组2 =(5,6,7,8)

组3 =(9,10,11,12)

的想法是使尽可能多的组合尽可能的2个成员(我想每个成员至少8个)没有与来自同一组的人匹配。

例如

1-5 1-6 1-7 1-8 1-9 1-10 1-11 1-12 2-5 2-6 2-7 2-8 2-9 ...... 等

这是一个代码段(它可能没有什么我想要实现的意义,但我是一个初级程序员)

<?php 

$numberSet = array(range(1,4), 
        range(5,8), 
        range(9,12) 
      ); 

$sizeofArray=count($numberSet); 

    for ($i=0; $i<$sizeofArray; $i++){ 
     for ($j=0; $j<count($numberSet[$i]); $j++){ 
      for ($k=0; $k<count($numberSet[$i]); $k++){ 
       echo $numberSet[$i][$j] . "<br>"; 
      } 
     } 
    } 
?> 
+0

所以你想最终显示所有可能的组合列表?或者你在寻找一组组合?或者你想要一个函数,传递一个ID并返回该ID的所有可能的组合? – Scoobler 2011-01-11 22:59:45

+0

谢谢你的回复!我想打印,因为我将发送清单给HR感谢:) – isJustMe 2011-01-11 23:11:41

回答

1

如果你清楚你实际上想要达到的是什么,它可能会有更多的帮助,但要继续下去,这里有一种方法可以获得一个组的成员的所有匹配,而不需要匹配它从自己的组中的任何 - 我会假设你计划有多个ID的,而不是一个简单的1234,5678,9 10 11 12在您的工作集:

// Build an example array: 
    $numberSet = array(range(1,4), 
         range(5,8), 
         range(9,12)); 

    // The function will return an array of matches when passed the array and the ID: 
    function findCombos($id, $set) 
    { 
     // Store the matches found: 
     $matches = array(); 
     // Loop through each array in the multidimensional array which was passed: 
     foreach ($set as $group) 
     { 
      // Make sure the ID passed isn't a member of the current array, don't want its matches: 
      if (!in_array($id, $group)) 
      { 
       // Loop through each array as the ID isn't a member of this group: 
       foreach ($group as $member) 
       { 
        // Add the match the the matches array: 
        $matches[] = $member; 
       } 
      } 
     } 
     // Pass the matches back: 
     return $matches; 
    } 

最后寻找单用户匹配:

// Find all the matches for ID 2 from the multidimensional array: 
    $matches = findCombos("2", $numberSet); 
    // Display the nubmer of matches: 
    echo "Found ".count($matches)." matches for 2.<br/>"; 
    // Loop through each match found: 
    foreach ($matches as $match) 
    { 
     // Display the results: 
     echo "2 - ".$match."<br/>"; 
    } 

结果:

Found 8 matches for 2. 
2 - 5 
2 - 6 
2 - 7 
2 - 8 
2 - 9 
2 - 10 
2 - 11 
2 - 12 

如果你想显示所有的可能性,你可以做这样的事情:

$count = 0; 
    foreach ($numberSet as $group) 
    { 
     foreach ($group as $member) 
     { 
      $matches = findCombos($member, $numberSet); 
      $count = $count+count($matches);  
      foreach ($matches as $match) 
      { 
       echo $member." - ".$match.", "; 
      } 
     } 
    } 
    echo "<br/>Found ".$count." possible combinations."; 

结果:

1 - 5,1 - 6,1 - 7,1 - 8, 1 - 9,1 - 10,1 - 11,1 - 12,2 - 5,2 - 6,2 - 7,2 - 8,2 - 9,2 - 10,2 - 11,2 - 12 ,3-5,3-6,3-7,3-8,3-9, 3-10,3-11,3-12,4-5,4-6, 4-7,4-8 ,4 - 9,4 - 10,4 - 11,4 - 12 5 - 1,5 - 2,5 - 3,5 - 4,5 - 9,5 - 10 5 - 11 5 - 12 6 - 1,6 - 2,6 - 3,6 - 4,6 - 9,6 - 10,6 - 11 6 - 12 7 - 1,7 - 2,7 - 3,7 - 4,7 - 9,7 - 10,7 - 11,7 - 12,8 - 1,8 - 2,8 - 3,8 - 4,8 - 9,8 - 10, 8 - 11 8 - 12 9 - 1,9 - 2,9 - 3,9 - 4,9 - 5,9 - 6,9 - 7,9 - 8,10 - 1,10 - 2,10 - 3,10 - 4,10 - 5,10 - 6,10 - 7,10 - 8,11 - 1,11 - 2,11 - 3,11 - 4,11 - 5,11 - 6,11 - 7,11 - 8,12 - 1,12 - 2 ,12 - 3,12 -4,12-5,12-6,12-7,12-8,

找到96种可能的组合。

如果chenage $ numberSet到:

$numberSet = array(array("a","b"), 
       array("c", "d", "e", "f"), 
       array("joe", "tom", "same") 
     ); 

结果:

A - C,A - d,A - E,A - F,A - 乔,一 - tom,a - 同样的,b - c,b - d,b - e,b - f,b - joe,b - tom,b - 相同,c - a,c - b,c - joe,c - tom,c - same, d - a,d - b,d - joe,d - tom,d - same,e - a,e - b,e - joe,e - tom, e - 同样,f - a,f - b,f - joe,f - tom,f - 相同,joe - a,joe - b,joe - c,joe - d,joe - e,joe - f ,tom-a, tom -b,tom -c,tom -d,tom -e, tom -f,same -a,same -b,same -c, same - d,same - e,same - f,

0

如果只计算2(而不是更高)的对,则可以简单地计算其他两个数组。

在阵列1人,简单地count(array2) + count(array3) = number of pairs

+0

顺便说一句,如果你保存你的数据在我想象的数据库中,这个过程会更容易。 – dqhendricks 2011-01-11 23:00:36

+0

感谢您的回应!我想我最终会这样做,但我希望首先得到算法谢谢! :) – isJustMe 2011-01-11 23:12:14

0

你可能想看看和array_diff()。我可以看到像这样的工作的内容:

$everyone=range(1,12); 
$groups=array(range(1,4), range(5,8), range(9,12)); 
$cnt=count($groups); 
for($i=0;$i<$cnt;$i++) { 
    // this will give you all the people who aren't in your group 
    $diff=array_diff($everyone,$groups[$i]); 

    // loop and compare here 
} 

是什么我不明白是,如果对“1-5”和“5-1”相同或不即你需要他们唯一对。