记录编号 2530 评测结果 AAAWAWAWWA
题目名称 算24点 最终得分 60
用户昵称 Gravatarelysian 是否通过 未通过
代码语言 Pascal 运行时间 0.004 s
提交时间 2008-09-22 20:22:34 内存使用 0.11 MiB
显示代码纯文本
program elysian;
const
fin='point24.in';fout='point24.out';
var
a:array[1..4] of integer;
c:array[1..3,1..3] of integer;
fh:array[1..3] of char;
used:array[1..9] of integer;
flag:boolean;
f1,f2:text;
i:integer;

procedure init;
var
i:integer;
begin
assign(f1,fin);reset(f1);
for i:=1 to 4 do
begin
read(f1,a[i]);
inc(used[a[i]]);
end;
close(f1);
assign(f2,fout);rewrite(f2);
end;

procedure print(ans:integer);
var
x:integer;
begin

if ans=24 then
begin
for x:=1 to 3 do
if c[x,1]>c[x,2] then begin write(f2,c[x,1],fh[x],c[x,2],'=');writeln(f2,c[x,3]);end
else begin write(f2,c[x,2],fh[x],c[x,1],'=');writeln(f2,c[x,3]);end;
flag:=true;
end;

end;

procedure dfs(x,num:longint);
var
i:integer;
begin
if flag=false then
begin
if x=4 then begin print(num);exit;end;

for i:=1 to 4 do
   if used[a[i]]>0 then
     begin
     c[x,1]:=num;  c[x,2]:=a[i]; c[x,3]:=num+a[i];fh[x]:='+';dec(used[a[i]]);
     dfs(x+1,num+a[i]);
     inc(used[a[i]]);

     if num-a[i]>=0 then
      begin
      c[x,1]:=num;  c[x,2]:=a[i]; c[x,3]:=num-a[i];fh[x]:='-';dec(used[a[i]]);
      dfs(x+1,num-a[i]);
      end;
     inc(used[a[i]]);

      c[x,1]:=num;  c[x,2]:=a[i]; c[x,3]:=num*a[i];fh[x]:='*';dec(used[a[i]]);
      dfs(x+1,num*a[i]);
      inc(used[a[i]]);

     if (a[i]<>0)and(num mod a[i] =0) then
      begin
      dec(used[a[i]]);
      c[x,1]:=num;  c[x,2]:=a[i]; c[x,3]:=num div a[i]; fh[x]:='/';
      dfs(x+1,num div a[i]);
      end;
inc(used[a[i]]);
 end;

end;

end;


begin
init;
for i:=1 to 4 do
begin
dec(used[a[i]]);
dfs(1,a[i]);
end;
if flag=false then writeln(f2,'No answer!');
close(f2);
end.