记录编号 2381 评测结果 WEEWWWEEWA
题目名称 算24点 最终得分 10
用户昵称 Gravatarlc 是否通过 未通过
代码语言 Pascal 运行时间 0.013 s
提交时间 2008-09-20 12:51:52 内存使用 0.11 MiB
显示代码纯文本
program day4_3;
 const
  oo:array[1..4] of char=('+','-','*','/');
 var
     i,n:integer;
     left,right,opp:array[1..7] of integer;
     sum,num:array[1..7] of integer;

 procedure print;
  var
     i:integer;
 begin
  for i:=5 to 7 do
   begin
   case opp[i] of
    1: num[i]:=num[left[i]]+num[right[i]];
    2: num[i]:=num[left[i]]-num[right[i]];
    3: num[i]:=num[left[i]]*num[right[i]];
    4: if num[left[i]]>num[right[i]]
       then num[i]:=num[left[i]] div num[right[i]]
       else num[i]:=num[right[i]] div num[left[i]];
   end; // case;
   if num[left[i]]<num[right[i]]
   then writeln(num[right[i]],oo[opp[i]],num[left[i]],'=',num[i])
   else writeln(num[left[i]],oo[opp[i]],num[right[i]],'=',num[i]);
   end;
  close(input); close(output);
  halt;
 end;


 procedure search(k:integer);
  var
      i,j,tmi,tmj,opt:integer;

  begin
   if k=1 then begin if sum[7]=24 then print; exit end;

   for i:=1 to 6 do
   if sum[i]<>-1
   then for j:=i+1 to 7 do
   if sum[j]<>-1
   then
       begin
       tmi:=sum[i]; tmj:=sum[j];
       for opt:=1 to 4 do
       begin
       n:=9-k;
       case opt of
        1: sum[n]:=sum[i]+sum[j];
        2: sum[n]:=sum[i]-sum[j];
        3: sum[n]:=sum[i]*sum[j];
        4:
          begin
          if sum[i]>sum[j]
          then begin
               if sum[i] mod sum[j]=0
               then sum[n]:=sum[i] div sum[j]
               end
          else
          if sum[j] mod sum[i]=0
          then sum[n]:=sum[j] div sum[i];
          end;
        end; // case
       left[n]:=i; right[n]:=j;
       sum[i]:=-1; sum[j]:=-1;
       opp[n]:=opt;
       search(k-1);

       sum[n]:=-1;
       sum[i]:=tmi; sum[j]:=tmj;
       // huan yuan;
       end; // opt;
      end;
  end;


 begin
  assign(input,'point24.in');
  assign(output,'point24.out');
  reset(input); rewrite(output);
  fillchar(sum,sizeof(sum),$FF);
  for i:=1 to 4 do
  read(sum[i]);
  num:=sum;
  n:=4;
  search(4);
  writeln('No answer');
  close(input); close(output);
 end.