Home  >  Article  >  Backend Development  >  PHP simple permutation and combination algorithm application example

PHP simple permutation and combination algorithm application example

怪我咯
怪我咯Original
2017-07-12 14:24:12925browse

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 &#39;<pre class="brush:php;toolbar:false">&#39;;
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!

Statement:
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