首页  >  文章  >  后端开发  >  PHP简单排列组合算法示例分享

PHP简单排列组合算法示例分享

小云云
小云云原创
2018-02-12 13:24:511661浏览

本文主要和大家介绍了PHP实现的简单排列组合算法,结合具体应用实例分析了排列组合算法的实现与使用技巧,需要的朋友可以参考下,希望能帮助到大家。

一、问题:

给你一个40斤的西瓜,给3个人分,有多少种分法?

二、PHP实现代码:


<?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;
?>

运行结果如下:


Array
(
  [0] => Array
    (
      [0] => 1
      [1] => 38
      [2] => 1
    )
  [1] => Array
    (
      [0] => 2
      [1] => 37
      [2] => 1
    )
  [2] => Array
    (
      [0] => 3
      [1] => 36
      [2] => 1
    )
  [3] => Array
    (
      [0] => 4
      [1] => 35
      [2] => 1
    )
  [4] => Array
    (
      [0] => 5
      [1] => 34
      [2] => 1
    )
  [5] => Array
    (
      [0] => 6
      [1] => 33
      [2] => 1
    )
  [6] => Array
    (
      [0] => 7
      [1] => 32
      [2] => 1
    )
  [7] => Array
    (
      [0] => 8
      [1] => 31
      [2] => 1
    )
  [8] => Array
    (
      [0] => 9
      [1] => 30
      [2] => 1
    )
  [9] => Array
    (
      [0] => 10
      [1] => 29
      [2] => 1
    )
  [10] => Array
    (
      [0] => 11
      [1] => 28
      [2] => 1
    )
  [11] => Array
    (
      [0] => 12
      [1] => 27
      [2] => 1
    )
  [12] => Array
    (
      [0] => 13
      [1] => 26
      [2] => 1
    )
  [13] => Array
    (
      [0] => 14
      [1] => 25
      [2] => 1
    )
  [14] => Array
    (
      [0] => 15
      [1] => 24
      [2] => 1
    )
  [15] => Array
    (
      [0] => 16
      [1] => 23
      [2] => 1
    )
  [16] => Array
    (
      [0] => 17
      [1] => 22
      [2] => 1
    )
  [17] => Array
    (
      [0] => 18
      [1] => 21
      [2] => 1
    )
  [18] => Array
    (
      [0] => 19
      [1] => 20
      [2] => 1
    )
  [19] => Array
    (
      [0] => 20
      [1] => 19
      [2] => 1
    )
  [20] => Array
    (
      [0] => 21
      [1] => 18
      [2] => 1
    )
  [21] => Array
    (
      [0] => 22
      [1] => 17
      [2] => 1
    )
  [22] => Array
    (
      [0] => 23
      [1] => 16
      [2] => 1
    )
  [23] => Array
    (
      [0] => 24
      [1] => 15
      [2] => 1
    )
  [24] => Array
    (
      [0] => 25
      [1] => 14
      [2] => 1
    )
  [25] => Array
    (
      [0] => 26
      [1] => 13
      [2] => 1
    )
  [26] => Array
    (
      [0] => 27
      [1] => 12
      [2] => 1
    )
  [27] => Array
    (
      [0] => 28
      [1] => 11
      [2] => 1
    )
  [28] => Array
    (
      [0] => 29
      [1] => 10
      [2] => 1
    )
  [29] => Array
    (
      [0] => 30
      [1] => 9
      [2] => 1
    )
  [30] => Array
    (
      [0] => 31
      [1] => 8
      [2] => 1
    )
  [31] => Array
    (
      [0] => 32
      [1] => 7
      [2] => 1
    )
  [32] => Array
    (
      [0] => 33
      [1] => 6
      [2] => 1
    )
  [33] => Array
    (
      [0] => 34
      [1] => 5
      [2] => 1
    )
  [34] => Array
    (
      [0] => 35
      [1] => 4
      [2] => 1
    )
  [35] => Array
    (
      [0] => 36
      [1] => 3
      [2] => 1
    )
  [36] => Array
    (
      [0] => 37
      [1] => 2
      [2] => 1
    )
  [37] => Array
    (
      [0] => 38
      [1] => 1
      [2] => 1
    )
)

相关推荐:

JS全排列组合算法实现方法

PHP简单排列组合算法的实现过程详解

浅谈PHP的排列组合的示例代码分享

以上是PHP简单排列组合算法示例分享的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn