This article mainly introduces the simple permutation and combination algorithm implemented in PHP, and analyzes the implementation and usage techniques of the permutation and combination algorithm based on specific application examples. Friends in need can refer to it. The details are as follows:
1. Question:
I give you a 40-pound watermelon and divide it among 3 people. How many ways are there to divide it?
2. PHP implementation code:
<?php $aa = range(1,40); $bb = array(); foreach($aa as $k=>$val){ foreach($aa as $v){ foreach($aa as $vl){ $sum = $val+$v+$vl; if($sum == 40){ $bb[$k][0] = $val; $bb[$k][1] = $v; $bb[$k][2] = $vl; } } } } echo '<pre class="brush:php;toolbar:false">'; print_r($bb); exit; ?>
The above is the detailed content of PHP simple permutation and combination algorithm application example. For more information, please follow other related articles on the PHP Chinese website!