PHP solution method of combination algorithm_PHP tutorial

WBOY
Release: 2016-07-21 15:21:53
Original
921 people have browsed it

Topic: Combination Algorithm: There is an array a with N elements. Now we are required to find the number of all combinations containing any element.
Answer: Let’s look at the rules first:
Assume this array is array(1,2,3,4,5) then M=5;
The possible combinations are:
1 number Number of combinations: 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
The number of combinations of 5 numbers: 1
It looks familiar, it is a 9*9 multiplication table in reverse order. Except for the M combinations in the first row, other combinations are processed according to the multiplication table, and the two FOR statements are nested
Code:

Copy code The 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;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324856.htmlTechArticleTitle: Combination Algorithm: There is an array a with N elements. Now we are required to find the array containing any element. All combinations. Answer: Let’s look at the rules first: Suppose this array is array(1...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!