Title: Combinatorial algorithm: There is an array a with n elements, and now it is required to find out the number of all combinations containing arbitrary elements.
Answer: Look at the law first:
Suppose that this array is m=5 as array (1,2,3,4,5);
The combinations that may appear are:
Number of combinations of 1 numbers: 5
Number of combinations of 2 numbers: 4+3+2+1
Number of combinations of 3 numbers: 3+2+1
Number of combinations of 4 numbers: 2+1
Number of combinations of 5 numbers: 1
It looks familiar, it is a 9*9 multiplication table in reverse order. Except that the first line has m combinations, the other combinations are handled by the multiplication table, and 2 for statements are nested.
Code:
Copy CodeThe code is as follows:
$c = 5;
$a = $c;
for ($i =1; $i <= $c; $i + +) {
for ($k = $c-$i; $k >0; $k –) {
$a + = $k;
}
}
echo $a;
http://www.bkjia.com/PHPjc/324856.html www.bkjia.com true http://www.bkjia.com/PHPjc/324856.html techarticle title: Combinatorial algorithm: There is an array a with n elements, and now it is required to find out the number of all combinations containing arbitrary elements. Answer: Look at the pattern first: Suppose this array is a arrays (1 ...