krot

перестановки комбинаций

Sep 11th, 2020 (edited)
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.48 KB | None | 0 0
  1. <?
  2. $n=3;//длина
  3. $m=2;
  4. $a=new SplFixedArray($n);
  5. $out=array();
  6. $used=new SplFixedArray($n+1);
  7.  
  8. rec(0);
  9. print_r($out);
  10. //count($out)=gmp_fact(n);
  11.  
  12. function out(){
  13.     global $a,$n,$out;
  14.     $tmp='';
  15.     for($i=0;$i<$n;++$i){
  16.         $tmp.=$a[$i];
  17.     }
  18.     $out[]=$tmp;
  19. }
  20. function rec($inx){
  21.     global $a,$m,$n,$used;
  22.     if($inx==$n){
  23.         out();
  24.         return;
  25.     }
  26.     for($i=1;$i<=$n;++$i){
  27.         if($used[$i])continue;
  28.         $a[$inx]=$i;
  29.         $used[$i]=true;
  30.         rec($inx+1);
  31.         $used[$i]=false;
  32.     }
  33. }
Add Comment
Please, Sign In to add comment