我有一个问题,一个应用程序,我正在开发的一个拼车计划,在我的公司(这个过程有点复杂)。我想做的是:
我有3支3人的队伍,每个队都有一个独特的成员身份,例如:
第1组= (1,2,3,4)
第2组= (5,6,7,8)
第3组= (9,10,11,12)
其想法是尽可能多地组合两个成员(我认为每个成员至少有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>";
}
}
}
?>发布于 2011-01-11 23:19:12
如果你弄清楚了你真正想要达到的目标,也许会有更多的帮助,但为了继续下去,这里有一种方法可以为一个组的成员获得所有的匹配,而不需要将它与其所属组中的任何人匹配--我将假设你计划在你的工作装置中有多个ID,而不是一个简单的1234、5678、91011 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- 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 - 512-6,12-7,12-8,
找到了96个可能的组合。
如果您将$numberSet添加到:
$numberSet = array( array("a","b"),
array("c", "d", "e", "f"),
array("joe", "tom", "same")
);结果:
a- c,a- d,a- e,a- f,a- joe,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-相同,d- a,d- b,d- joe,d- tom,d-相同,e- a,e-joe-joe-tom-e-同,f- a,f-f- b,a-a,c- b,d- joe,d- tom,d-a,e-a。F- joe,f- tom,f-同,joe - a,joe - b,joe,joe - e,joe,tom - a,tom - b,tom - c,tom - d,tom - e,tom - f,同- a,同- b,同- c,同- d,同- e,同- f,
发布于 2011-01-11 22:59:30
如果它只计算2的对(而不是更高的),您可以简单地数另外两个数组。
对于array1中的任何人,只需使用count(array2) + count(array3) = number of pairs
发布于 2011-01-11 23:10:37
您可能想看看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”是否相同,也就是说,你需要它们成为唯一的一对。
https://stackoverflow.com/questions/4663519
复制相似问题