当前位置:Gxlcms > PHP教程 > 高分求一个算法,在线等待中...

高分求一个算法,在线等待中...

时间:2021-07-01 10:21:17 帮助过:2人阅读

本帖最后由 sibang 于 2013-10-16 22:08:17 编辑

',Var_Dump($x3),'
';/**期望的?果是: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),'
';/**期望的?果是: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','fh','fi',}/**/


回复讨论(解决方案)

好多小花花

$arr=Array(    Array('a','b','c'),    Array('d','e','f'),    Array('g','h','i'),    //第二?array的??是未知的,?了方便?例所以只?了3?); print_r(iArray($arr,3));print_r(iArray($arr,2)); function iArray($ar, $n) {  if($n == 2) { //3取2的组合    $ar = array(      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(    Array('a','b','c'),    Array('d','e','f'),    Array('g','h','i'),    //第二?array的??是未知的,?了方便?例所以只?了3?); print_r(iArray($arr,3));print_r(iArray($arr,2)); function iArray($ar, $n) {  if($n == 2) { //3取2的组合    $ar = array(      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(
Array('a','b','c','j'),
Array('d','e','f'),
Array('g','h','i'),
Array('k','l','m'),
);

也可能是:
$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的取值??是: 2~6

我在代码中已经提示了要先求一下 M取N 的组合
而求组合的函数相信你的代码库中已经是有的
这是我用的

// 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;}

所以 iArray 函数就可写作
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;}

人气教程排行