比赛 NOIP_4 评测结果 WAWWWWAAWA
题目名称 算24点 最终得分 40
用户昵称 maxiem 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-09-19 22:08:52
显示代码纯文本
program point24;
type node=record
  a,b,o,sum:shortint;
end;
var
  n:array [1..7] of shortint;
  step:array [1..3] of node;
  i:byte;
procedure print;
var i:byte;
begin
  for i:=1 to 3 do begin
    if step[i].sum<0 then write (step[i].a) else if (step[i].a>step[i].b) then write (step[i].a) else write (step[i].b);
    case step[i].o of
      1:write ('+');
      2:write ('-');
      3:write ('*');
      4:write ('/');
    end;
    if step[i].b<0 then write ('(',step[i].b,')') else
      if step[i].sum<0 then write (step[i].b) else if step[i].a>step[i].b then write (step[i].b) else write (step[i].a);
    writeln ('=',step[i].sum);
  end;
  close (output);
  halt;
end;
procedure go(s:byte);
var ca,cb,a,b,o:shortint;
begin
  if (s=4) and (step[3].sum=24) then print else begin
    for a:=1 to 7 do if n[a]<>0 then for b:=1 to 7 do if (n[b]<>0) and (a<b) then for o:=1 to 4 do begin
      step[s].a:=n[a];step[s].b:=n[b];step[s].o:=o;
      case o of
        1:step[s].sum:=step[s].a+step[s].b;
        2:step[s].sum:=step[s].a-step[s].b;
        3:step[s].sum:=step[s].a*step[s].b;
        4:if step[s].a/step[s].b-step[s].a div step[s].b=0 then step[s].sum:=step[s].a div step[s].b;
      end;
      if step[s].sum<>0 then begin
        ca:=n[a];n[a]:=0;cb:=n[b];n[b]:=0;
        n[4+s]:=step[s].sum;
        go(s+1);
        n[a]:=ca;n[b]:=cb;n[4+s]:=0;
      end;
      step[s].a:=0;step[s].b:=0;step[s].o:=0;step[s].sum:=0;
    end;
  end;
end;
begin
  assign (input,'point24.in');
  reset (input);
  fillchar (n,sizeof(n),0);
  fillchar (step,sizeof(step),0);
  for i:=1 to 4 do read (n[i]);
  close (output);
  assign (output,'point24.out');
  rewrite (output);
  go(1);
  writeln ('No answer!');
  close (output);
end.