라엘이꺼
라엘이 머리속 알고리즘으로 짰다. PHP 내장함수를 사용하지 않아서
C나 C++, JAVA등 다른 코드로 응용변환이 쉬울것이다.
<?
class caserow{
private $user_select = 0; //사용자의 선택값 저장
private $str_count = 0; //현재 줄 번호 출력용
private $str_char = Array(0=>'1',1=>'2',2=>'3',3=>'4',4=>'5',5=>'6',6=>'7',7=>'8'); //출력 할 문자조합
public function caserow($cnt = 0){
$this->user_select = $cnt; //선택값을 저장
$this->get_combination();
}
private function get_combination($start = NULL,$end = NULL){
if($start == NULL) $start = 0;
if($end == NULL) $end = $this->user_select - 1;
$p = &$this->str_char;
if($start == $end){
$this->print_case($p);
}
else{
for ($i=$start;$i<=$end;$i++) //시작배열부터 끝 배열까지 반복
{
$this->str_swap($p[$start],$p[$i]);
$this->get_combination($start+1,$end);
$this->str_swap($p[$start],$p[$i]);
}
}
}
private function str_swap(&$str1,&$str2){
$temp = $str1;
$str1 = $str2;
$str2 = $temp;
}
private function print_case($p = Array()){
$this->str_count ++;
echo "{$this->str_count} \t";
for($i=0;$i<$this->user_select;$i++){
echo $p[$i];
}
echo "\n";
}
}
if(isset($_GET['input'])){
$inputnum = $_GET['input'];
}
else{
$inputnum = 4;
}
if(!is_numeric($inputnum) || $inputnum < 1 || $inputnum > 8) $inputnum = 4;
echo "<pre>\n";
$caserow = new caserow($inputnum);
echo "</pre>";
?>
너구리님꺼
http://install.dppia.com/numberofcases.php
/*!
* \fn NumberOfCases
* \brief 경우의수를 출력하는 재귀함수
꼭 숫자가 아니더라도 어떤 문자든지 가능함
* \param $arr_number 경우의수로 쓰일 숫자(문자) 배열
예) 1~7까지의 경우의 수 (1,2,3,4,5,6,7)
예) 1,3,5,7 경우의 수 (1,3,5,7)
* \param $used 재귀함수에 쓰이는 파라메터 사용된 수를 저장하는 배열이다.
*/
function NumberOfCases($arr_number, $used=array()) {
foreach($arr_number as $v) {
if(in_array($v, $used)) continue;
$used[] = $v;
if(sizeof($arr_number)==sizeof($used)) {
echo implode('',$used).'<br/>';
return;
}
NumberOfCases($arr_number, $used);
array_pop($used);
}
}
유창화님꺼
http://cdn01.lael.be/u/
<?php
function show_all_numbers(&$num, $show=1, $array=Array(), $use=Array()){
if (count($array) > 0) {
foreach($array as $k => $v){
$array2 = $array;
unset($array2[$k]);
$use2 = $use;
$use2[] = $v;
show_all_numbers($num, $show, $array2, $use2);
}
}
else {
$num[] = $use;
if ($show == 1) echo count($num) . '. ' . implode(', ', $use) . "<br>\n";
}
}
$range = 4;//받은 숫자
$array = range(1, $range);
$num = Array();
show_all_numbers($num, 1, $array);
?>
결론 : 셋중 당신이 봤을 때 쉬운게 좋은 코드입니당~

• Comment List
2011/08/25 11:50 [Modify/Delete] [Reply]
안녕하세요 유창화입니다.
그누보드에서 글 왜 지우셧어요?
무슨 일 있나요?
좋은 내용이라고 생각하는데...
2011/08/25 17:30 [Modify/Delete]
곧 비슷한 글로 다시 작성할께요.
제 개인정보가 드러나는 것을 싫어해서요.