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 the Code code as follows:
$c = 5;
$a = $c;
for ($i =1; $i <= $c; $i + +) {
for ($k = $c-$i; $k >0; $k –) {
$a + = $k;
}
}
echo $a;
The above describes the Photoshop CS2 v9.0 Green Chinese version of the combination algorithm PHP solution, including the Photoshop CS2 v9.0 Green Chinese version of the content, I hope that the PHP tutorial interested in a friend helpful.