求这种的算法
比如我现在有一个数组
$array=Array("a",'b','c','d','e','f','g');
当然不止这么几个
我想要输出的结果是:
a
b
c
d
e
f
g
aa
ab
ac
ad
ae
af
ag
ba
bb
bc
bd
be
bf
bg
ca
cb
cc
…………
gaaaaaaa
gaaaaaab
gaaaaaac
gaaaaaad
gaaaaaae
…………
就是把这些字符能组成的字符串都罗列出来
回复讨论(解决方案)
LZ的问题好像有点歧义吧,比如a这个字母
a算一个 aa算一个 aaa算一个 aaaaaaaaaaaaaaaaaaaaaaaaaa也是一个
要多少是结束啊?
比如我现在有一个数组
$array=Array("a",'b','c','d','e','f','g');
当然不止这么几个
我想要输出的结果是:
a
b
c
d
e
f
g
aa
ab
ac
ad
ae
af
ag
ba
bb
bc
bd
be
bf
bg
ca
cb
cc
…………
gaaaaaaa
gaaaaaab
gaaaaaac
gaaaaaad
gaaaaaae
…………
就是把这些字符能组成的字符串都罗列出来
没问题的,就是要无限个啊。。。
$array=Array("a",'b','c','d','e','f','g');
for($i=-1;$i
echo $array[$i].$v."
";
}
}
希望能帮到你~
那您这程序啥时候停止啊....
没问题的,就是要无限个啊。。。
那您这程序啥时候停止啊....
没问题的,就是要无限个啊。。。
拔下电源的时候...
估计是想做对撞机
那您这程序啥时候停止啊....
没问题的,就是要无限个啊。。。
拔下电源的时候...
虽然楼主需求看上去有些无厘头,但如果设定好终止条件也并非不能实现
在有限个数的前提下,这个算法还是很具挑战性的
我先来的抛砖引玉
$ar = array("a",'b','c');print_r(allot($ar, 4));print_r(allot($ar, 2));function allot($ar, $num) { static $st = array(); //用于缓存中间结果 $res = array(); //用于承载返回结果 if(! $st) { $t = $ar; }else { $t = array(); foreach($ar as $v) { $t = array_merge($t, array_map( function($item, $prefix) { return $prefix . $item; }, $st, array_fill(0, count($st), $v))); } }// foreach($t as $v) echo $v . PHP_EOL; //直接产生输出 $res = $st = $t; if($num > 1) $res = array_merge($res, allot($ar, $num-1)); else $st = array(); //初始缓存,以便下一次调用 return $res;}
(
[0] => a
[1] => b
[2] => c
[3] => aa
[4] => ab
[5] => ac
[6] => ba
[7] => bb
[8] => bc
[9] => ca
[10] => cb
[11] => cc
[12] => aaa
[13] => aab
[14] => aac
[15] => aba
[16] => abb
[17] => abc
[18] => aca
[19] => acb
[20] => acc
[21] => baa
[22] => bab
[23] => bac
[24] => bba
[25] => bbb
[26] => bbc
[27] => bca
[28] => bcb
[29] => bcc
[30] => caa
[31] => cab
[32] => cac
[33] => cba
[34] => cbb
[35] => cbc
[36] => cca
[37] => ccb
[38] => ccc
[39] => aaaa
[40] => aaab
[41] => aaac
[42] => aaba
[43] => aabb
[44] => aabc
[45] => aaca
[46] => aacb
[47] => aacc
[48] => abaa
[49] => abab
[50] => abac
[51] => abba
[52] => abbb
[53] => abbc
[54] => abca
[55] => abcb
[56] => abcc
[57] => acaa
[58] => acab
[59] => acac
[60] => acba
[61] => acbb
[62] => acbc
[63] => acca
[64] => accb
[65] => accc
[66] => baaa
[67] => baab
[68] => baac
[69] => baba
[70] => babb
[71] => babc
[72] => baca
[73] => bacb
[74] => bacc
[75] => bbaa
[76] => bbab
[77] => bbac
[78] => bbba
[79] => bbbb
[80] => bbbc
[81] => bbca
[82] => bbcb
[83] => bbcc
[84] => bcaa
[85] => bcab
[86] => bcac
[87] => bcba
[88] => bcbb
[89] => bcbc
[90] => bcca
[91] => bccb
[92] => bccc
[93] => caaa
[94] => caab
[95] => caac
[96] => caba
[97] => cabb
[98] => cabc
[99] => caca
[100] => cacb
[101] => cacc
[102] => cbaa
[103] => cbab
[104] => cbac
[105] => cbba
[106] => cbbb
[107] => cbbc
[108] => cbca
[109] => cbcb
[110] => cbcc
[111] => ccaa
[112] => ccab
[113] => ccac
[114] => ccba
[115] => ccbb
[116] => ccbc
[117] => ccca
[118] => cccb
[119] => cccc
)
Array
(
[0] => a
[1] => b
[2] => c
[3] => aa
[4] => ab
[5] => ac
[6] => ba
[7] => bb
[8] => bc
[9] => ca
[10] => cb
[11] => cc
)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.
