Back to writing
미분류 3 min read HYEONG HWAN, MUN

경우의 수 php 코드

PHP로 경우의 수를 짜봅시다.

라엘이꺼
라엘이 머리속 알고리즘으로 짰다. PHP 내장함수를 사용하지 않아서

C나 C++, JAVA등 다른 코드로 응용변환이 쉬울것이다.

 

[code language="php"]
[/code]

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*!
 *  \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);
   }  
}

유창화님꺼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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);
?>

결론 : 셋중 당신이 봤을 때 쉬운게 좋은 코드입니당~