Problems related to a permutation and combination algorithm

Source: Internet
Author: User
An arrangement and combination algorithm has been learned in middle school. & nbsp; arrangement and combination & nbsp; 1 & nbsp; 2 & nbsp; 3 & nbsp; 4 & nbsp; 5 & nbsp; 6. you can construct 720 different strings. here we write a rough implementation method & nbsp; for example, & nbsp; 1-& nbsp; 6. Which of the following experts can you advise; question about a permutation and combination algorithm
In middle school, I learned how to arrange and combine 1, 2, 3, 4, 6, and can form 720 different strings.
Here we write a rough implementation method example: 1-6
Hope you can give me some advice.
$ J = 0;
For ($ I = 123456; $ I <= $ number; $ I ++ ){
$ Box = array ();

$ Ge = $ I % 10;
If ($ ge> 6 | $ ge = 0 ){
Continue;
}
$ Box [] = $ ge;
$ Shi = intval ($ I/10) % 10;
If ($ shi> 6 | $ shi = 0 ){
Continue;
}
$ Box [] = $ shi;
$ Bai = intval ($ I/100) % 10;
If ($ bai> 6 | $ bai = 0 ){
Continue;
}
$ Box [] = $ bai;
$ Qian = intval ($ I/1000) % 10;
If ($ qian> 6 | $ qian = 0 ){
Continue;
}
$ Box [] = $ qian;
$ Wan = intval ($ I/10000) % 10;
If ($ wan> 6 | $ wan = 0 ){
Continue;
}
$ Box [] = $ wan;
$ Shiwan = intval ($ I/100000 );
If ($ shiwan> 6 | $ shiwan = 0 ){
Continue;
}
$ Box [] = $ shiwan;
// Echo count (array_unique ($ box ));
// Echo'
';
// Var_dump (array_unique ($ box ));
// Echo'
';
If (count (array_unique ($ box ))! = 6 ){
Continue;
}

If ($ j % 10 = 0 ){
Echo" ";
}
$ J ++;
Echo '', $ I ,'';
}
Echo" ";
Echo "{$ j} in total ";
------ Solution --------------------
$a = perm(array(1,2,3,4,5,6));
echo count($a);
//print_r($a);

function perm($list, $k=0, $m=0) {     
  if(! $m) $m = count($list) - 1; 
  $r = array();    
  if($k >= $m) {          
    $r[] = join('', $list);         
  }else {         
    for($i = $k; $i <= $m; $i++) {             
      list($list[$k], $list[$i]) = array($list[$i], $list[$k]);
      $r = array_merge($r, perm($list, $k + 1, $m));             
      list($list[$k], $list[$i]) = array($list[$i], $list[$k]);
    }
  }
  return $r;
}

720

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.