
พอดีว่า... ว่างจัดครับ 55+
ขอไปโพสไว้ที่ pastebin อีกที : http://pastebin.com/gqx9nGXW
sub solution {
my @num=@_;
my @opr=('+','-','*','/');
my $step;
my $output;
my $exp;
my $a;
my $b;
my $c;
foreach $step (1..8) { #loop for order of operator
foreach $a (0..3) { #loop for math operator
foreach $b (0..3) { #loop for math operator
foreach $c (0..3) { #loop for math operator
if ($step==1||$step==4||$step==6||$step==7) { $output.='('; }
$output.=$num[0];
$output.=$opr[$a];
if ($step==2||$step==3) { $output.='('; }
$output.=$num[1];
if ($step==1||$step==6) { $output.=')'; }
$output.=$opr[$b];
if ($step==1||$step==5) { $output.='('; }
$output.=$num[2];
if ($step==2||$step==4) { $output.=')'; }
$output.=$opr[$c];
$output.=$num[3];
if ($step==1||$step==3||$step==5||$step==7) { $output.=')'; }
$exp=eval($output);
if ($exp==24) {
++$j;
print "[$j] :: $output\n";
}
$output='';
}
}
}
}
}
my $a;
my $b;
my $c;
my $d;
my @num;
$j=0;
if ($^O eq 'MSWin32') { system('cls'); }
else { system('clear'); }
print "\n === [24 Points : Solution] ===\n";
print " By AssazziN [14/05/2011] \n\n";
print " Enter number (Ex. 1 3 4 6) : ";
chomp($input=);
($num[0],$num[1],$num[2],$num[3])=$input=~/^(\d)\s(\d)\s(\d)\s(\d)$/;
unless ($num[0]&&$num[1]&&$num[2]&&$num[3]) { print "Format error !"; exit(0); }
foreach $a (0..3) {
foreach $b (0..3) {
if ($b==$a) { next; }
foreach $c (0..3) {
if ($c==$a||$c==$b) { next; }
foreach $d (0..3) {
if ($d==$a||$d==$b||$d==$c) { next; }
solution($num[$a],$num[$b],$num[$c],$num[$d]);
}
}
}
}
print "Not found solution !!\n" if ($j==0);
## Code Grean Grean By AssazziN
เขียนเสร็จก็งงกับตัวเองครับ อ่านแล้วงงงงยังไงไม่รู้ ฮ่าๆๆ
เสริม ภาพประกอบตอนทำครับ ตอนใส่วงเล็บนะ ตรงบรรทัด foreach $step (1..
