Home  >  Article  >  php教程  >  php通过排列组合实现1到9数字相加都等于20的方法

php通过排列组合实现1到9数字相加都等于20的方法

WBOY
WBOYOriginal
2016-06-06 19:48:521711browse

这篇文章主要介绍了php通过排列组合实现1到9数字相加都等于20的方法,实例分析了php排列组合数学运算的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php通过排列组合实现1到9数字相加都等于20的方法。分享给大家供大家参考。具体实现方法如下:

3+8+9 [1] => 4+7+9 测试效果:1:对于加数数组比较小的,速度可以,过大的话,有些慢;2:每次可以使用不同的加数的,处理会变慢 采用的方法是:生成所有可能排列,对排列处理过滤重复的,,得到组合 */ function huoqu_zhuhe($eq,$jiashu,$isone=1) {if(empty($jiashu)||!is_array($jiashu)){echo 'error:加数必须数组';return false;} $feishu=0; for($i=0;$i$eq)break; else if(($nowhe+$jiashu[$j])==$eq){ $savearr[]=$savestr; } else{$newarr[$k]=$savestr;$k++;} }//end for($j=0;$j

运行结果如下:

Array ( [0] => 3+8+9 [1] => 4+7+9 [2] => 5+6+9 [3] => 5+7+8 [4] => 1+2+8+9 [5] => 1+3+7+9 [6] => 1+4+6+9 [7] => 1+4+7+8 [8] => 1+5+6+8 [9] => 2+3+6+9 [10] => 2+3+7+8 [11] => 2+4+5+9 [12] => 2+4+6+8 [13] => 2+5+6+7 [14] => 3+4+5+8 [15] => 3+4+6+7 [16] => 1+2+3+5+9 [17] => 1+2+3+6+8 [18] => 1+2+4+5+8 [19] => 1+2+4+6+7 [20] => 1+3+4+5+7 [21] => 2+3+4+5+6 )

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

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