[PHP] Permutation

<?php

class tomato {
   static $myArray;
   
   function tomato($str) {
   $this->generate(str_split($str));
   $this->myArray = $this->multi_unique($this->myArray);
   }
   
   function generate($arr1, $arr2=array()) {
   if(Empty($arr1)) {
   $this->myArray[] = $arr2;
   } else {
   $len = count($arr1) - 1;
   for ($i = $len ; $i >= 0; --$i) {
   $arg1 = $arr1;
   $arg2 = $arr2;
   list($arg) = array_splice($arg1, $i, 1);
   array_unshift($arg2, $arg);
   $this->generate($arg1, $arg2);
   }
   return $this->myArray;
   }
   }
   
   function multi_unique($arr) {
   foreach ($arr as $k=>$val) 
   $new[$k] = serialize($val);

   $arr = array_unique($new);

   foreach($arr as $k=>$val)
   $new1[] = unserialize($val);

   return $new1;
   }
   
   function printIt() {
   for($i = 0; $i<count($this->myArray); $i++) {
   echo implode("", $this->myArray[$i])."<br>";
   }
   }
}

$code = new tomato("1244");
$code->printIt();

?>

看了 juzhax 貼的 “在PHP里其中一个Permutation方程式” , 覺得用數組的方式定義好排列伸縮性就只能在長度4的情況下產生。

所以寫了個recursive的方法﹐計算上也不會花太多時間。有意思的人自己去測試看看吧。

不错不错,谢谢高手分享。加油加油!

why hide it now ?

see see pls

很好,终于遇到高手了。

see… thanks

看看下!!

hav a look~

cc…谢谢分享哦 Y^^

thanks for share

不错不错,谢谢高手分享。加油加油!

Permutation

can’t handle big alphanumeric

thanks …

juz looking forward

引用第12楼microsys2006-10-20 12:11 PM发表的“”:
can’t handle big alphanumeric

哈哈,那麽大的numeric要算甚麽啊?
一般大的計算就不會用網頁了

我那学学。。。。。

我那学学。。。。。

permutation?…参考参考

看看阿是否好玩!!!