class Combinations implements Iterator { protected $c = null; // Combination of numbers protected $s = null; // Source array protected $n = 0; // Number of elements in the array protected $k = 0; // Number of elements in each combination protected $pos = 0; // Current position of the iterator function __construct($s, $k) { // Initialize the class properties if(is_array($s)) { $this->s = array_values($s); $this->n = count($this->s); } else { $this->s = (string) $s; $this->n = strlen($this->s); } $this->k = $k; $this->rewind(); } // Return the current key function key() { return $this->pos; } // Return the current value function current() { $r = array(); for($i = 0; $i k; $i ) $r[] = $this->s[$this->c[$i]]; return is_array($this->s) ? $r : implode('', $r); } // Move to the next combination function next() { if($this->_next()) $this->pos ; else $this->pos = -1; } // Rewind to the first combination function rewind() { $this->c = range(0, $this->k); $this->pos = 0; } // Check if the iterator is valid (at a valid position) function valid() { return $this->pos >= 0; } // Move to the next combination (internal function) protected function _next() { $i = $this->k - 1; while ($i >= 0 && $this->c[$i] == $this->n - $this->k $i) $i--; if($i c[$i] ; while($i k - 1) $this->c[$i] = $this->c[$i - 1] 1; return true; } } // Create a Combinations object for the given array and number of elements per combination $combinations = new Combinations("1234567", 5); // Iterate over all possible combinations and print them out foreach($combinations as $substring) echo $substring, ' ';
class Combinations implements Iterator { protected $c = null; // Combination of numbers protected $s = null; // Source array protected $n = 0; // Number of elements in the array protected $k = 0; // Number of elements in each combination protected $pos = 0; // Current position of the iterator function __construct($s, $k) { // Initialize the class properties if(is_array($s)) { $this->s = array_values($s); $this->n = count($this->s); } else { $this->s = (string) $s; $this->n = strlen($this->s); } $this->k = $k; $this->rewind(); } // Return the current key function key() { return $this->pos; } // Return the current value function current() { $r = array(); for($i = 0; $i k; $i ) $r[] = $this->s[$this->c[$i]]; return is_array($this->s) ? $r : implode('', $r); } // Move to the next combination function next() { if($this->_next()) $this->pos ; else $this->pos = -1; } // Rewind to the first combination function rewind() { $this->c = range(0, $this->k); $this->pos = 0; } // Check if the iterator is valid (at a valid position) function valid() { return $this->pos >= 0; } // Move to the next combination (internal function) protected function _next() { $i = $this->k - 1; while ($i >= 0 && $this->c[$i] == $this->n - $this->k $i) $i--; if($i c[$i] ; while($i k - 1) $this->c[$i] = $this->c[$i - 1] 1; return true; } } // Create a Combinations object for the given array and number of elements per combination $combinations = new Combinations("1234567", 5); // Iterate over all possible combinations and print them out foreach($combinations as $substring) echo $substring, ' ';
반복자 인터페이스를 구현하고 주어진 숫자의 가능한 모든 조합을 중단 할 수있는 방법을 제공합니다. 작동 방식은 다음과 같습니다. 클래스 조합은 반복자를 구현합니다 { 보호 된 $ c = null; // 숫자의 조합 보호 된 $ s = null; // 소스 배열 보호 된 $ n = 0; // 배열의 요소 수 보호 된 $ k = 0; // 각 조합의 요소 수 보호 된 $ pos = 0; // 반복자의 현재 위치 함수 __construct ($ s, $ k) { // 클래스 속성을 초기화합니다 if (is_array ($ s)) { $ this-> s = array_values ($ s); $ this-> n = count ($ this-> s); } 또 다른 { $ this-> s = (문자열) $ s; $ this-> n = strlen ($ this-> s); } $ this-> k = $ k; $ this-> rewind (); } // 현재 키를 반환합니다 함수 키 () { $ this-> pos; } // 현재 값을 반환합니다 함수 current () { $ r = array (); for ($ i = 0; $ i k; $ i) $ r [] = $ this-> s [$ this-> c [$ i]]; return is_array ($ this-> s)? $ r : Implode ( '', $ r); } // 다음 조합으로 이동합니다 기능 다음 () { if ($ this-> _ next ()) $ this-> pos; 또 다른 $ this-> pos = -1; } // 첫 번째 조합으로 되감기 함수 rewind () { $ this-> c = 범위 (0, $ this-> k); $ this-> pos = 0; } // 반복자가 유효한지 확인하십시오 (유효한 위치에) function valid () { $ this-> pos> = 0을 반환합니다. } // 다음 조합으로 이동 (내부 기능) 보호 기능 _next () { $ i = $ this-> k -1; while ($ i> = 0 & amp; & amp; $ this-> c [$ i] == $ this-> n- $ this-> k $ i) $ i-; if ($ i c [$ i]; while ($ i k -1) $ this-> c [$ i] = $ this-> c [$ i-1] 1; 진실을 반환하십시오. } } // 주어진 배열에 대한 조합 개체 생성 및 조합 당 요소 수 $ 조합 = 새로운 조합 ( "1234567", 5); // 가능한 모든 조합을 반복하여 인쇄하십시오. foreach ($ combinations as $ substring) echo $ substring, ';
이 코드는 다음 출력을 생성합니다. 24567 34567
부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.
Copyright© 2022 湘ICP备2022001581号-3