The example in this article tells you how PHP can add 1 to 9 numbers equal to 20 by arranging a combination. Share to everyone for your reference. The implementation method is as follows:
<?php set_time_limit (0); /* Function Description: Huoqu_zhuhe ($eq, $jiashu, $isone =0) parameter description: $eq---sum of several numbers; $jiashu-------addends array: $jiashu =array (1,2,3,4,5,6,7,8,9 ), the addends can be used; $isone---whether to use different addends each time, uniqueness, 1 is 0 no, default 1 return type: array, number with + concatenated string: [0] => 3+8+9 [1] => 4+7+9 Test Effect: 1: For addends array relatively small, speed can be With, too big, a little slow; 2: Each time you can use a different addends, the process slows down by: generate all possible permutations, filter the permutation to duplicate, get the combined/function Huoqu_zhuhe ($eq, $jiashu, $isone =1) {if ( Empty ($jiashu) | |!
Is_array ($jiashu)) {echo ' ERROR: Addends must be array '; return false;} $feishu = 0; For ($i =0 $i <count ($jiashu); $i + +) {if (!is_numeric ($jiashu [$i])) {$feishu =1;break;}} if ($feishu ==1) {echo ' Error
; The array must be a valid number '; return false;}
$lian = $jiashu;
$savearr =array (); while (!empty ($lian)) {//echo 1; $newarr =array (), $k =0 for ($i =0; $i <count ($lian); $i + +) {$lianstr = $lian [$i]; $arr =
Explode (' + ', $lianstr);
$nowhe =array_sum ($arr);
Echo $nowhe; For ($j =0 $j <count ($jiashu); $j + +) {$savestr = $lianstr. '
+ '. $jiashu [$j];
if ($isone ==1&&in_array ($jiashu [$j], $arr)) continue;
if ($nowhe + $jiashu [$j]) > $eq) break; else if ($nowhe + $jiashu[$j] = = $eq) {$savearr []= $savestr;} else{$newarr [$k]= $savestr; $k + +;}} $j =0 $j <count ($jiashu)}//end for ($i =0; $i $lian = $newarr;}
End while (!empty ($lian))//print_r ($savearr); Generate the composition part, the filter repeats, 2 arrays take one as the reference, see whether another can achieve the match through the movement, can, filter $isguolu =array ();//store the corresponding ID of the trade-off 0 take 1 for ($i =0; $i <count ($savearr); $i + +) {$isguolu []=0;} Initializes all 0 for ($i =0; $i <count ($savearr), $i + +) {$arr 1=explode (' + ', $savearr [$i]), $len 1=count ($arr 1), for ($j = $i +1; $j <count ($savearr); $j + +) {$arr 2=explode (' + ', $savearr [$j]); $len 2=count ($arr 2); if ($len 1!= $len 2) continue; if ($
isguolu[$j]==1) continue;
Comparison $arr1 and $arr2 start $jishu = 0; For ($i 1=0 $i 1<count ($arr 1), $i 1++) {$a = $arr 1[$i 1]; $isyou =0; for ($i 2= $i 1; $i 2<count ($arr 2); $i 2++) {if ($a ==$ arr2[$i 2]) {$jishu + +; $isyou =1; $t = $arr 2[$i 1]; $arr 2[$i 1]= $arr 2[$i 2]; $arr 2[$i 2]= $t; break;}} End to ($i 2=0 if ($isyou ==0) break; End for ($i 1=0 $i 1<count ($arr 1), if ($jishu = = $len 1) $isguolu [$j]=1;} End for ($j = $i +1;} End for ($i =0; $i <count ($savearr); $i + +)//print_r ($isguoLU);
Select $newarr =array () According to the filter array;
For ($i =0 $i <count ($savearr); $i + +) {if ($isguolu [$i]==0) $newarr []= $savearr [$i];}//print_r ($newarr);
return $newarr;
//Below is a test//fetch with 1,2,3,4,5,6,7,8,9 add all equals 20 combination $jiashu =array (1,2,3,4,5,6,7,8,9);
$eq = 20;
if ($jieguo =huoqu_zhuhe ($eq, $jiashu, 1)) Print_r ($jieguo);?>
The results of the operation are as follows:
Array
(
[0] => 3+8+9
[1] => 4+7+9
[2] => 5+6+9
[3] => 5+7+8
[4] => 1+2+8+9
[5 ] => 1+3+7+9
[6] => 1+4+6+9
[7] => 1+4+7+8
[8] => 1+5+6+8
[9] => 2+3+6+9
[a] => 2+3+7+8
[one] => 2+4+5+9
[a] => 2+4+6+8
[a] => 2+5+6+7
[a] => 3+4+5+8
[a] => 3+4+6+7
[[] => 1+2+3+5+9
[a] => 1+2+3+6+8
[a] => 1+2+4+5+8
[] => 1+2+4+6+7
[ ] => 1+3+4+5+7
[] => 2+3+4+5+6
)
I hope this article will help you with your PHP program design.