给定一列字符,生成指定长度的所有可能的组合
2016-02-20 16:14
597 查看
给定一列字符,生成指定长度的所有可能的组合:
如:a,b,c,d,e 或 0-9
生成长度 1:a, b, c, d, e; 长度2 :aa, ab, ac, ad, ae, ba, bb, bc, bd, be,................ee
<?php
function de($len, $pos = 0) {
static $bit = [];
static $source = ['a', 'b', 'c', 'd', 'e'];/*[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];*/
$pos++;
for($i = 0; $i < count($source); $i++) {
$bit[$pos] = $source[$i];
if ($pos < $len) {
de($len, $pos);
} else {
echo implode('', $bit)."\n";
}
}
}
用phpcmd小助手(https://github.com/dclnet/phpcmd)运行代码
![](https://img-blog.csdn.net/20160220162046594?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
以上为长度为1
![](https://img-blog.csdn.net/20160220162118412?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
长度为2的。
如:a,b,c,d,e 或 0-9
生成长度 1:a, b, c, d, e; 长度2 :aa, ab, ac, ad, ae, ba, bb, bc, bd, be,................ee
<?php
function de($len, $pos = 0) {
static $bit = [];
static $source = ['a', 'b', 'c', 'd', 'e'];/*[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];*/
$pos++;
for($i = 0; $i < count($source); $i++) {
$bit[$pos] = $source[$i];
if ($pos < $len) {
de($len, $pos);
} else {
echo implode('', $bit)."\n";
}
}
}
用phpcmd小助手(https://github.com/dclnet/phpcmd)运行代码
以上为长度为1
长度为2的。
相关文章推荐
- 设置grub密码
- Linux C语言应用 - main函数解析,时间函数,C语言代码的分配机制(malloc,calloc,realloc,free)
- HDU-1097-A hard puzzle( 快速幂取模 )
- Groovy spread-dot operator
- AutoComplete.js输入智能提示插件
- Jmeter之Bean shell使用(二)
- 读取assets目录下的图片
- js中的arguments
- unity 解析xml
- js中的小问题
- Jmeter之Bean shell使用(一)
- RequireJS 与 SeaJS 的异同
- ubuntu初探2----对软件的操作命令
- poj 2367 Genealogical tree
- 【每日算法】排序算法总结(复杂度&稳定性)
- PHP首部认证
- Android Binder实现的进程间IPC过程概要
- Android Binder实现的进程间IPC过程概要
- 解决fatal error C1859: “Debug\thread5.pch”意外的预编译头错误的方法
- VLFeat-使用matlab版本计算HOG