比赛 NOIP_4 评测结果 WAWWWWAAWA
题目名称 算24点 最终得分 40
用户昵称 elysian 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-09-19 21:32:50
显示代码纯文本
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 boolean;
flag:boolean;
f1,f2:text;
i:integer;

procedure init;
var
i:integer;
begin
assign(f1,fin);reset(f1);
for i:=1 to 4 do
read(f1,a[i]);
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 x=4 then begin print(num);exit;end;

for i:=1 to 4 do
   if used[a[i]]=false then
     begin
     c[x,1]:=num;  c[x,2]:=a[i]; c[x,3]:=num+a[i]; fh[x]:='+';used[a[i]]:=true;
     dfs(x+1,num+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]:='-';used[a[i]]:=true;
      dfs(x+1,num-a[i]);
      end;


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


     if (a[i]<>0)and(num mod a[i] =0) then used[a[i]]:=true;
      begin
      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;
used[a[i]]:=false;
 end;

end;




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