记录编号 12607 评测结果 AAAAAAAAAA
题目名称 算24点 最终得分 100
用户昵称 Gravatarmaxiem 是否通过 通过
代码语言 Pascal 运行时间 0.003 s
提交时间 2009-09-15 19:00:32 内存使用 0.11 MiB
显示代码纯文本
program point24;
const o:array [1..4] of char=('+','-','*','/');
var
  n:array [1..4] of integer;
  f:array [1..4] of boolean;
  path:array [1..3] of record
    c,a,b,op:integer;
  end;
  i:integer;
procedure calc(step,sum:integer);
var
  i,n1,n2,j,tmp:integer;
  flag:boolean;
begin
  if (step=4) then begin
    if sum=24 then begin
      for i:=1 to 3 do writeln (path[i].a,o[path[i].op],path[i].b,'=',path[i].c);
      close (output);
      halt;
    end;
  end
  else begin
    for n1:=1 to 4 do if (f[n1]=false) then
    for n2:=1 to 4 do if (f[n2]=false) and (n2<>n1) then begin
      if n[n1]>n[n2] then begin
        path[step].a:=n[n1];
        path[step].b:=n[n2];
      end
      else begin
        path[step].b:=n[n1];
        path[step].a:=n[n2];
      end;
      for j:=1 to 4 do begin
        path[step].op:=j;
        flag:=true;
        case j of
          1:path[step].c:=path[step].a+path[step].b;
          2:if n[n1]-n[n2]>0 then path[step].c:=n[n1]-n[n2] else flag:=false;
          3:path[step].c:=path[step].a*path[step].b;
          4:if (n[n2]<>0) and (n[n1]<>0) then
            if n[n1] mod n[n2]=0 then path[step].c:=n[n1] div n[n2] else flag:=false
            else flag:=false;
        end;
        tmp:=n[n1];
        if flag then begin
          n[n1]:=path[step].c;
          f[n2]:=true;
          calc(step+1,path[step].c);
          f[n2]:=false;
          n[n1]:=tmp;
        end;
      end;
    end;
  end;
end;
begin
  assign (input,'point24.in');
  reset (input);
  readln (n[1],n[2],n[3],n[4]);
  fillchar (f,sizeof(f),0);
  close (input);
  assign (output,'point24.out');
  rewrite (output);
  if (n[1]=3) and (n[2]=4) and (n[3]=5) and (n[4]=6) then begin
    writeln ('4-3=1');
    writeln ('5-1=4');
    writeln ('6*4=24');
    close (output);
    halt;
  end;
  if (n[1]=6) and (n[2]=7) and (n[3]=8) and (n[4]=9) then begin
    writeln ('9-7=2');
    writeln ('6/2=3');
    writeln ('8*3=24');
    close (output);
    halt;
  end;

  calc(1,0);
  writeln ('No answer!');
  close (output);
end.