이것은 당신의 조건을 만족하는 것 같다 http://codepad.viper-7.com/WHkQeD
<?php
$num_arrays = 10; $num_elements = 4;
$min = 1; $max = 20;
$exclude_numbers = array(1, 4, 6); // Add numbers here to exclude
$answer = array();
for($i = 0; $i < $num_arrays; $i++)
{
$answer[$i] = array();
for($j = 0; $j < $num_elements; $j++)
{
do
{
$current = rand($min, $max);
// If the previous array exists and there are more than two common elements when we add the $current element, continue
if(isset($answer[$i-1]) && count(array_intersect($answer[$i-1], array_merge($answer[$i], array($current))) > 2)
{
continue;
}
} while(in_array($current, $exclude_numbers) || in_array($current, $answer[$i]));
$answer[$i][$j] = $current;
}
}
var_dump($answer);
편집 : 여기이 조건을 모두 만족하는 완벽한 솔루션입니다. 같은
Demo
<?php
$num_arrays = 10; $num_elements = 4;
$min = 1; $max = 20;
$exclude_numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
$answer = array();
for($i = 0; $i < $num_arrays; $i++)
{
$answer[$i] = array();
for($j = 0; $j < $num_elements; $j++)
{
do
{
// Get a random element
$current = rand($min, $max);
$new_array = array_merge($answer[$i], array($current));
// If the previous array has more than two common elements (because of the added $current), get a new $current
if(isset($answer[$i-1]) && count(array_intersect($answer[$i-1], $new_array)) > 2)
{
$answer[$i] = array_diff($new_array, $answer[$i-1]);
$j = count($answer[$i]) - 1;
continue;
}
} while(in_array($current, $exclude_numbers) || in_array($current, $answer[$i]));
$answer[$i][$j] = $current;
// If the array is complete, we need to check for unique arrays
if(count($answer[$i]) == $num_elements)
{
$k = $i - 1;
while($k >= 0)
{
if(count(array_diff($answer[$k], $answer[$i])) == 0)
{
// This array is the same as a previous one, start over
$answer[$i] = array();
$j = -1;
break;
}
$k--;
}
// Optionally sort each array
sort($answer[$i]);
}
}
}
var_dump($answer);
이 숙제인가? – prodigitalson
저는 선택할 수있는 20 개의 자식 요소가있을 때 최대 4 개의 고유 한 자식 요소가있는 10 개의 부모 요소가있는 다중 딤 배열을 갖는 것이 가능한지 이해하지 못합니다. 이 작업을 수행하려면 상위 요소 당 최대 2 개의 하위 요소 (총 20 개, 총 배열 요소 10 개) 만 가질 수 있습니다. 그렇지 않으면 처음 5 개의 상위 요소를 4 개의 하위 요소로 채우면 채워지지 않은 5 개의 상위 요소가 생깁니다. –
@Digital Precision @Digital Precision 이것은 완전히 가능합니다. 현재 행은 이전 행의 2 개 이상을 매치하지 않아야하며, 고유 한 것으로 단지 두 행이 다른 순서로있는 한 동일한 번호를 사용할 수 있음을 의미합니다. – dqhendricks