PHP is a combination of 1 to 9 numbers equal to 20 of the method, the PHP permutation
This example describes how PHP can add 1 to 9 numbers equal to 20 by arranging combinations. Share to everyone for your reference. The implementation method is as follows:
<?phpset_time_limit (0);/* Function Description: Huoqu_zhuhe ($eq, $jiashu, $isone =0) parameter description: $eq---sum of several numbers; $jiashu-------addend array: $ Jiashu=array (1,2,3,4,5,6,7,8,9), which can be used addend; $isone---whether to use a different addend each time, uniqueness, 1 is 0 no, the default 1 return type: array, number with + concatenated string: [0] = = 3+8+9 [1] = > 4+7+9 Test Results: 1: For the addend array is small, the speed can, too big, some slow; 2: Each time you can use a different addend, the process will be slow to use the method is: to generate all possible permutations, filtering on the arrangement of duplicate, get the combination */function huoqu_ Zhuhe ($eq, $jiashu, $isone =1) {if (empty ($jiashu) | |! Is_array ($jiashu)) {echo ' ERROR: Addend must array '; return false;} $feishu =0;for ($i =0; $i
$EQ) Break;else if (($nowhe + $jiashu [$j]) = = $eq) {$savearr []= $savestr;} else{$newarr [$k]= $savestr; $k + +;}} End for ($j =0; $j
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 [ten] = 2+3+7+8< C11/>[11] = 2+4+5+9 [[] = 2+4+6+8 [] = 2+5+6+7 [+] = 3+4+5+8 [] = 3+4+6+7< C16/>[16] = 1+2+3+5+9 [+] = 1+2+3+6+8 [+] = 1+2+4+5+8 [+] = 1+2+4+6+7 [+] = 1+3+4+5+7 [+] = 2+3+4+5+6)
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1042687.html www.bkjia.com true http://www.bkjia.com/PHPjc/1042687.html techarticle PHP is a combination of 1 to 9 of the number is equal to 20 of the method, the PHP permutation of the example of this article describes PHP through the permutation of the combination of 1 to 9 numbers equals 20 of the method. Share to the big ...