PHP simple permutation and combination algorithm application example

怪我咯
Release: 2023-03-13 17:54:02
Original
924 people have browsed it

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;
?>
Copy after login

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!

Related labels:
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!