记录编号 18481 评测结果 AAAAAAAAAA
题目名称 算24点 最终得分 100
用户昵称 Gravatar苏轼 是否通过 通过
代码语言 Pascal 运行时间 0.022 s
提交时间 2010-09-15 13:28:35 内存使用 0.12 MiB
显示代码纯文本
program point24(input,output);

var
  n:array[1..4]of integer;
  s:array[1..3]of string;
  s1,s2:string;
  i,tmp:integer;

function go(sum,deep:integer):boolean;
  var
    i,flag,ans,t:integer;
    boo:boolean;
    ch:char;
  begin
    ch:=#0;

    if deep<4 then
    begin
      inc(deep);

      for i:=1 to 4 do
        if n[i]<>0 then
        begin
          flag:=n[i];
          n[i]:=0;

          if go(sum+flag,deep) then
          begin
            ans:=sum+flag;
            ch:='+';
          end
          else if (sum>=flag) and go(sum-flag,deep) then
          begin
            ans:=sum-flag;
            ch:='-';
          end
          else if (sum<flag) and go(flag-sum,deep) then
          begin
            ans:=flag-sum;
            ch:='-';
            boo:=true;
          end
          else if go(sum*flag,deep) then
          begin
            ans:=sum*flag;
            ch:='*';
          end
          else if (sum mod flag=0)and go(sum div flag,deep) then
          begin
            ans:=sum div flag;
            ch:='/';
          end
          else if (sum<>0)and(flag mod sum=0)and go(flag div sum,deep) then
          begin
            ans:=flag div sum;
            ch:='/';
            boo:=true;
          end;

          n[i]:=flag;

          if ch<>#0 then
          begin
            flag:=i;
            break;
          end;
        end;
    end
    else
      exit(sum=24);

    dec(deep);
    if ch<>#0 then
    begin
      str(sum,s1);
      str(n[i],s2);
      str(ans,s[deep]);

      if sum>n[i] then
        s[deep]:=s1+ch+s2+'='+s[deep]
      else
        s[deep]:=s2+ch+s1+'='+s[deep];

      exit(true);
    end
    else
      exit(false);
  end;

begin
  assign(input,'point24.in');
  reset(input);

  assign(output,'point24.out');
  rewrite(output);

  readln(n[1],n[2],n[3],n[4]);

  for i:=1 to 4 do
  begin
    tmp:=n[i];
    n[i]:=0;

    if go(tmp,1) then
      break;

    n[i]:=tmp;
  end;

  if s[1]<>'' then
    for i:=1 to 3 do
      writeln(s[i])
  else
    writeln('No answer!');

  close(input);
  close(output);
end.