比赛 NOIP_4 评测结果 EEEEEEEEEE
题目名称 算24点 最终得分 0
用户昵称 MayLava 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-09-19 21:24:35
显示代码纯文本
program MayLava;
const
 ch:array[1..4]of char=('+','-','*','/');
var
 a,b,c:array[1..4]of integer;
 d:array[1..4]of boolean;
procedure start;
 var
  f:text;
 begin
  assign(f,'d:\point24.in');
  reset(f);
  readln(f,a[1],a[2],a[3],a[4]);
  close(f);
  fillchar(d,sizeof(d),true);
  fillchar(b,sizeof(b),0);
  fillchar(c,sizeof(c),0);
 end;
procedure print;
 var
  i,j,sw:integer;
  f:text;
 begin
  assign(f,'point24.out');
  rewrite(f);
  for i:=1 to 3 do begin
   if a[i]<a[j] then begin sw:=a[i]; a[i]:=a[j]; a[j]:=sw; end;
   write(f,a[i],ch[c[i+1]],a[i+1],'=');
   case c[i+1] of
    1: begin a[i+1]:=a[i]+a[j]; writeln(f,a[i+1]); end;
    2: begin a[i+1]:=a[i]-a[j]; writeln(f,a[i+1]); end;
    3: begin a[i+1]:=a[i]*a[j]; writeln(f,a[i+1]); end;
    4: begin a[i+1]:=a[i] div a[j]; writeln(f,a[i+1]); end;
   end;
  end;
  close(f);
  halt;
 end;
procedure hunt(p,tot:integer);
 var
  i,j:integer;
 begin
  if (p>4) then begin
   if tot=24 then print;
   exit;
  end;
  for i:=1 to 4 do begin
   if d[i] then begin
    d[i]:=false;
    b[p]:=a[i];
    for j:=1 to 4 do
     case j of
      1:begin c[p]:=j; hunt(p+1,a[p-1]+a[p]); end;
      2:begin c[p]:=j; hunt(p+1,a[p-1]-a[p]); end;
      3:begin c[p]:=j; hunt(p+1,a[p-1]*a[p]); end;
      4:if (a[p-1] div a[p])=(a[p-1]/a[p]) then begin
          c[p]:=j;
          hunt(p+1,a[p-1]+a[p]);
         end;
     end;
    d[i]:=true;
    end;
  end;
 end;
procedure main;
 var
  i:integer;
  f:text;
 begin
  for i:=1 to 4 do begin
   d[i]:=false;
   b[1]:=a[i];
   hunt(2,a[i]);
   d[i]:=true;
  end;
  assign(f,'point24.out');
  rewrite(f);
  writeln(f,'No answer!');
  close(f);
 end;
BEGIN
 start;
 main;
END.