Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.86 KB | None | 0 0
  1. sub solution {
  2.     my @num=@_;
  3.     my @opr=('+','-','*','/');
  4.     my $step;
  5.     my $output;
  6.     my $exp;
  7.     my $a;
  8.     my $b;
  9.     my $c;
  10.     foreach $step (1..8) {                                      #loop for order of operator
  11.         foreach $a (0..3) {                                      #loop for math operator
  12.             foreach $b (0..3) {                                      #loop for math operator
  13.                 foreach $c (0..3) {                                      #loop for math operator
  14.                     if ($step==1||$step==4||$step==6||$step==7) { $output.='('; }
  15.                     $output.=$num[0];
  16.  
  17.                     $output.=$opr[$a];
  18.                     if ($step==2||$step==3) { $output.='('; }
  19.                     $output.=$num[1];
  20.                     if ($step==1||$step==6) { $output.=')'; }
  21.  
  22.                     $output.=$opr[$b];
  23.                     if ($step==1||$step==5) { $output.='('; }
  24.                     $output.=$num[2];
  25.                     if ($step==2||$step==4) { $output.=')'; }
  26.  
  27.                     $output.=$opr[$c];
  28.                     $output.=$num[3];
  29.                     if ($step==1||$step==3||$step==5||$step==7) { $output.=')'; }
  30.                     $exp=eval($output);
  31.                
  32.                     if ($exp==24) {
  33.                         ++$j;
  34.                         print "[$j]   ::   $output\n";
  35.                     }
  36.                     $output='';
  37.                 }
  38.             }
  39.         }
  40.     }
  41. }
  42.  
  43. my $a;
  44. my $b;
  45. my $c;
  46. my $d;
  47. my @num;
  48. $j=0;
  49.  
  50. if ($^O eq 'MSWin32') { system('cls'); }
  51. else { system('clear'); }
  52. print "\n   === [24 Points : Solution] ===\n";
  53. print "    By AssazziN [14/05/2011] \n\n";
  54. print "   Enter number (Ex. 1 3 4 6) : ";
  55. chomp($input=<STDIN>);
  56. ($num[0],$num[1],$num[2],$num[3])=$input=~/^(\d)\s(\d)\s(\d)\s(\d)$/;
  57. unless ($num[0]&&$num[1]&&$num[2]&&$num[3]) { print "Format error !"; exit(0); }
  58.  
  59.  
  60.  
  61. foreach $a (0..3) {
  62.     foreach $b (0..3) {
  63.         if ($b==$a) { next; }
  64.         foreach $c (0..3) {
  65.             if ($c==$a||$c==$b) { next; }
  66.             foreach $d (0..3) {
  67.                 if ($d==$a||$d==$b||$d==$c) { next; }
  68.                 solution($num[$a],$num[$b],$num[$c],$num[$d]);
  69.             }
  70.         }
  71.     }
  72. }
  73. print "Not found solution !!\n" if ($j==0);
  74.  
  75. ## Code Grean Grean By AssazziN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement