欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP实现的简单排列组合算法应用示例

程序员文章站 2024-03-11 10:40:37
本文实例讲述了php实现的简单排列组合算法应用。分享给大家供大家参考,具体如下: 一、问题: 给你一个40斤的西瓜,给3个人分,有多少种分法? 二、php实现代码:...

本文实例讲述了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 '<pre>';
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
    )
)

更多关于php相关内容感兴趣的读者可查看本站专题:《php数据结构与算法教程》、《php基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。