Home  >  Article  >  Backend Development  >  5组数字,每组取一个组成不相同的5位数,大神

5组数字,每组取一个组成不相同的5位数,大神

WBOY
WBOYOriginal
2016-06-23 13:26:351308browse

例 : 012345678,013456789,023456789,012456789,023456789
这5组数每组取一个组成不相同的5位数


回复讨论(解决方案)

$d = explode(',', '012345678,013456789,023456789,012456789,023456789');$r = array();for($i=0; $i<count($d); $i++) {  for($j=0; $j<strlen($d[$i]); $j++)    if(! in_array($d[$i]{$j}, $r)) {      $r[] = $d[$i]{$j};      break;    }}echo join(' ', $r);
你可能需要有随机性的
$d = explode(',', '012345678,013456789,023456789,012456789,023456789');$r = array();for($i=0; $i<count($d); $i++) {  $loop = 100;  do {    $j = rand(0, strlen($d[$i]) - 1);    if(! in_array($d[$i]{$j}, $r)) {      $r[] = $d[$i]{$j};      break;    }  }while($loop--);}echo join(' ', $r);

我是想把所有能组成的5位数都显示出来,我没写清楚

循环 去重呗

$d = array_map('str_split', explode(',', '012345678,013456789,023456789,012456789,023456789'));print_r(foo($d));function foo($d) {  $r = array_pop($d);  while($d) {    $t = array();    foreach(array_pop($d) as $k1=>$x) {      foreach($r as $k2=>$y) $t[] = "$x $y";    }    $r = $t;  }  return $r;}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn