Calculate an algorithm with high scores. wait online... This post was last edited by sibang at 22:08:17, January 16 ,.
', Var_Dump ($ x3 ),'
';/** Expected? Result: array {'adg', 'Adh ', 'Adi', 'AEG ', 'aeh', 'aei', 'afg', 'afh ', 'afi', 'bdg', 'bdh', 'bdi ', 'beg', 'beh', 'bei', 'BFG', 'bfh', 'BFI ', 'cdg', 'cdh', 'cdi', 'ceg', 'ceh', 'cei', 'cfg ', 'cfh', 'cfi ',} /**/$ x2 = iArray ($ arr, 2); Echo'
',Var_Dump($x2),'
';/** Expected? Result: array {'AD', 'AE', 'AF', 'AG', 'AH', 'Ai', 'bd ', 'be ', 'BF', 'bg ', 'Bh', 'bi', 'CD', 'Ce ', 'cf', 'cg', 'CH', 'ci ', 'dg ', 'DH', 'di', 'EG ', 'eh', 'Ei ', 'fg', 'ff ',} /**/
Reply to discussion (solution)
A lot of flowers
$ Arr = Array ('A', 'B', 'C'), Array ('D', 'e', 'F '), array ('G', 'H', 'I'), // second? Array ?? Is unknown ,? Is it convenient? For example, only? 3 ?); Print_r (iArray ($ arr, 3); print_r (iArray ($ arr, 2); function iArray ($ ar, $ n) {if ($ n = 2) {// 3 get the combination of 2 $ ar = array ($ ar [0], $ ar [1]), array ($ ar [0], $ ar [2]), array ($ ar [1], $ ar [2]),);} else $ ar = array ($ ar ); $ res = array (); foreach ($ ar as $ d) {$ r = array_pop ($ d); while ($ d) {$ t = array (); foreach (array_pop ($ d) as $ x) foreach ($ r as $ y) $ t [] = $ x. $ y; $ r = $ t;} $ res = array_merge ($ res, $ r);} return $ res ;}
Array
(
[0] => adg
[1] => adh
[2] => adi
[3] => aeg
[4] => aeh
[5] => aei
[6] => afg
[7] => afh
[8] => afi
[9] => bdg
[10] => bdh
[11] => bdi
[12] => beg
[13] => beh
[14] => bei
[15] => bfg
[16] => bfh
[17] => bfi
[18] => cdg
[19] => cdh
[20] => cdi
[21] => ceg
[22] => ceh
[23] => cei
[24] => cfg
[25] => cfh
[26] => cfi
)
Array
(
[0] => ad
[1] => AE
[2] => af
[3] => bd
[4] => be
[5] => bf
[6] => cd
[7] => ce
[8] => cf
[9] => ag
[10] => ah
[11] => ai
[12] => bg
[13] => bh
[14] => bi
[15] => cg
[16] => ch
[17] => ci
[18] => dg
[19] => dh
[20] => di
[21] => eg
[22] => eh
[23] => ei
[24] => fg
[25] => fh
[26] => fi
)
$ Arr = Array ('A', 'B', 'C'), Array ('D', 'e', 'F '), array ('G', 'H', 'I'), // second? Array ?? Is unknown ,? Is it convenient? For example, only? 3 ?); Print_r (iArray ($ arr, 3); print_r (iArray ($ arr, 2); function iArray ($ ar, $ n) {if ($ n = 2) {// 3 get the combination of 2 $ ar = array ($ ar [0], $ ar [1]), array ($ ar [0], $ ar [2]), array ($ ar [1], $ ar [2]),);} else $ ar = array ($ ar ); $ res = array (); foreach ($ ar as $ d) {$ r = array_pop ($ d); while ($ d) {$ t = array (); foreach (array_pop ($ d) as $ x) foreach ($ r as $ y) $ t [] = $ x. $ y; $ r = $ t;} $ res = array_merge ($ res, $ r);} return $ res ;}
Array
(
[0] => adg
[1] => adh
[2] => adi
[3] => aeg
[4] => aeh
[5] => aei
[6] => afg
[7] => afh
[8] => afi
[9] => bdg
[10] => bdh
[11] => bdi
[12] => beg
[13] => beh
[14] => bei
[15] => bfg
[16] => bfh
[17] => bfi
[18] => cdg
[19] => cdh
[20] => cdi
[21] => ceg
[22] => ceh
[23] => cei
[24] => cfg
[25] => cfh
[26] => cfi
)
Array
(
[0] => ad
[1] => AE
[2] => af
[3] => bd
[4] => be
[5] => bf
[6] => cd
[7] => ce
[8] => cf
[9] => ag
[10] => ah
[11] => ai
[12] => bg
[13] => bh
[14] => bi
[15] => cg
[16] => ch
[17] => ci
[18] => dg
[19] => dh
[20] => di
[21] => eg
[22] => eh
[23] => ei
[24] => fg
[25] => fh
[26] => fi
)
?? It may be:
$ Arr = Array (
Array ('A', 'B', 'C', 'J '),
Array ('D', 'e', 'F '),
Array ('G', 'H', 'I '),
Array ('K', 'L', 'M '),
);
It may also be:
$ Arr = Array (
Array ('A', 'B', 'C', 'J '),
Array ('D', 'e', 'F '),
Array ('G', 'H', 'I '),
Array ('K', 'L', 'M '),
Array ('2', '1', '3', 'SD', 'ee '),
);
IArray ($ ar, $ n)
$ N value ?? Yes: 2 ~ 6
In the code, I have already indicated that you need to first find the combination of M and N.
The combined functions are believed to already exist in your code library.
This is what I use
// Combination function combination ($ arr, $ num = 0) {$ len = count ($ arr); if ($ num = 0) $ num = $ len; $ res = array (); for ($ I = 1, $ n = pow (2, $ len); $ I <$ n; ++ $ I) {$ tmp = str_pad (base_convert ($ I, 10, 2), $ len, '0', STR_PAD_LEFT); $ t = array (); for ($ j = 0; $ j <$ len; ++ $ j) {if ($ tmp {$ j} = '1 ') {$ t [] = $ arr [$ j] ;}} if (count ($ t) ==$ num) $ res [] = $ t ;} return $ res ;}
So iArray functions can be written.
function iArray($ar, $n) { $res = array(); foreach(combination($ar, $n) as $d) { $r = array_pop($d); while($d) { $t = array(); foreach(array_pop($d) as $x) foreach($r as $y) $t[] = $x . $y; $r = $t; } $res = array_merge($res, $r); } return $res;}