<?php function number($num,$a,$b){ //返回两数计算的结果 switch ($num) { case 1: return $a+$b; break; case 2: return $a-$b; break; case 3: return $a*$b; break; case 4: if($b==0){ return 0; }else{ return $a/$b; } break; } } function number1($num){ //返回符号 switch ($num) { case 1: return "+"; break; case 2: return "-"; break; case 3: return "*"; break; case 4: return "/"; break; } } function allcount($a,$b,$c,$d){ //echo 为24的值 for($i=1;$i<=4;$i++){ for($j=1;$j<=4;$j++){ for($k=1;$k<=4;$k++){ $result = number($k,number($j,number($i,$a,$b),$c),$d); if($result==24){ echo "(((".$a.number1($i).$b.")".number1($j).$c.")".number1($k).$d.")=".$result."<br>"; //exit; } $result = number($k,number($i,$a,$b),number($j,$c,$d)); if($result==24){ echo "(".$a.number1($i).$b.")".number1($k)."(".$c.number1($j).$d.")=".$result."<br>"; //exit; } } } } } $a=array(); $a[1]=6; $a[2]=6; $a[3]=6; $a[4]=6; //4位数的组合 for($i=1;$i<=4;$i++){ for($j=1;$j<=4;$j++){ if($i!=$j){ for($k=1;$k<=4;$k++){ if($k!=$j&&$k!=$i){ for($n=1;$n<=4;$n++){ if($n!=$j&&$n!=$i&&$n!=$k){ allcount($a[$i],$a[$j],$a[$k],$a[$n]); } } } } } } } ?>