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

求这种的算法

程序员文章站 2022-04-07 22:05:10
...
比如我现在有一个数组
$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 foreach($array as $k=>$v){
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;}
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
[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
)
相关标签: 求这种的算法