在弄一个组合的有多少种的可能性。
有7个数组(列),如a、b、c、d、e、f、g,每个数列约100-200不等元素
每次从7个数组(列)取出一个元素,按顺序(即第1组元素排最前面,第7组元素排最后面)组成一个组合。
要求任意两个组合中,至少有5个元素不重复,这样的组合最多有多少种?
求大虾写出编程代码,我自己写出的代码,经常会崩溃掉,才浅。
我这边随便搞了个,原数组有7个
然后用的冒泡算的,会计算21次,然后显示出有效的次数和无效的次数
最后会打印出哪次对比是有效的
不是写死的,每次都是会刷新的
<?php
function myfunction($a,$b){
if ($a===$b){
return 0;
}
return ($a>$b)?1:-1;
}
$list_obj = array(
array(1,2,3,4,5,6,7,8,9,10),
array(1,2,3,4,5,6,7,8,9,10),
array(1,2,3,4,5,6,7,8,9,10),
array(1,2,3,4,5,6,7,8,9,10),
array(1,2,3,4,5,6,7,8,9,10),
array(1,2,3,4,5,6,7,8,9,10),
array(1,2,3,4,5,6,7,8,9,10)
);
$new_list = array();
$curr_list = array();
for ($i=0; $i < 7; $i++) {
for ($j=0; $j < 7; $j++) {
array_push($curr_list,$list_obj[$j][rand(0,9)]);
}
array_push($new_list,$curr_list);
echo "<br>";
$curr_list = array();
}
print("<pre>"); // 格式化输出数组
print_r($new_list);
print("</pre>");
$all_get = 0;
$all_not = 0;
for ($i=0; $i < 7; $i++) {
for ($j=$i + 1; $j < 7; $j++) {
$result=array_uintersect($new_list[$i],$new_list[$j],"myfunction");
$index = number_format(count($result));
var_dump($result);
if($index > 2){
echo "无效";
$all_not += 1;
}else{
echo "有效";
$all_get += 1;
}
echo $index."<br>";
}
}
echo "有效的集合总共有".$all_get."个<br>";
echo "无效的集合总共有".$all_not."个<br>";
?>
本题主,会积极采纳有帮助的答案,也可私信追加。
下面是我最初的版本,能直观到我的本意。但数值太大,会崩溃的,后面改进的版本,也不尽理想
<?php
$alls = [];
for($a=1; $a<=255; $a++){
$t = array();
$t[0] = $a;
for($b=1; $b<=157; $b++){
$t[1] = $b;
if (check_arr($alls,$t)){
continue;
}
for($c=1; $c<=147; $c++){
$t[2] = $c;
if (check_arr($alls,$t)){
continue;
}
for($d=1; $d<=165; $d++){
$t[3] = $d;
if (check_arr($alls,$t)){
continue;
}
for($e=1; $e<=176; $e++){
$t[4] = $e;
if (check_arr($alls,$t)){
continue;
}
for($f=1; $f<=145; $f++){
$t[5] = $f;
if (check_arr($alls,$t)){
continue;
}
for($g=1; $g<=143; $g++){
$t[6] = $g;
if (!check_arr($alls,$t)){
$alls[] = $t;
unset($t[1]);
unset($t[2]);
unset($t[3]);
unset($t[4]);
unset($t[5]);
unset($t[6]);
break 5;
}
}
}
}
}
}
}
}
function check_arr($alls, $t, $n=1){ /*$n,可重复的元素*/
for($i=0; $i<count($alls); $i++){
$l = 0;
if (isset($alls[$i])) {
for ($p=0;$p<count($t);$p++){
if ($alls[$i][$p] == $t[$p]) $l++;
}
}
if ($l>$n) return true;
}
return false;
}
echo "组合数:".count($alls);
echo "<pre>";
?>