记录编号 |
19116 |
评测结果 |
AAAAAAAAAA |
题目名称 |
算24点 |
最终得分 |
100 |
用户昵称 |
nick09 |
是否通过 |
通过 |
代码语言 |
Pascal |
运行时间 |
0.004 s |
提交时间 |
2010-09-28 21:49:12 |
内存使用 |
0.11 MiB |
显示代码纯文本
program asf;
var
n,g,ans:array[1..4]of integer;
m,an:array[1..4]of char;
f:text;
s,a,b,c,d,p1,p2,p3:integer;
procedure init;
var i:integer;
begin
assign(f,'point24.in');reset(f);
for i:=1 to 4 do read(f,g[i]);
close(f);
end;
procedure print;
var i,j:integer;
begin
assign(f,'point24.out');rewrite(f);
for i:=1 to 3 do
begin
if i=1 then begin
if n[1]>=n[2]then writeln(f,n[1],m[1],n[2],'=',ans[1])
else writeln(f,n[2],m[1],n[1],'=',ans[1])
end
else
begin
if ans[i-1]>=n[i+1]
then writeln(f,ans[i-1],m[i],n[i+1],'=',ans[i])
else writeln(f,n[i+1],m[i],ans[i-1],'=',ans[i])
end;
end;
close(f);halt;
end;
function go(k1,k2:integer;u:char):boolean;
begin
go:=true;
if u='+' then s:=k1+k2;
if u='-' then
if k1>k2 then s:=k1-k2
else go:=false;
if u='*' then s:=k1*k2;
if u='/' then
if (k1 mod k2 =0)and(k1>=k2) then s:=k1 div k2
else go:=false;
end;
function check:boolean;
var i,j,k:integer;
fg:boolean;
begin
check:=false;
s:=n[1];
j:=0;
repeat
fg:=true;
inc(j);
if go(s,n[j+1],m[j])=false then fg:=false
else ans[j]:=s;
if fg=false then exit;
until j=3;
if s=24 then begin ans[4]:=24;check:=true;end;
end;
procedure search(q,w,e,r:integer);
var i,j,k:integer;
begin
n[1]:=q;n[2]:=w;n[3]:=e;n[4]:=r;
for i:=1 to 4 do
for j:=1 to 4 do
for k:=1 to 4 do
begin
if i=1 then m[1]:='+';
if i=2 then m[1]:='-';
if i=3 then m[1]:='*';
if i=4 then m[1]:='/';
if j=1 then m[2]:='+';
if j=2 then m[2]:='-';
if j=3 then m[2]:='*';
if j=4 then m[2]:='/';
if k=1 then m[3]:='+';
if k=2 then m[3]:='-';
if k=3 then m[3]:='*';
if k=4 then m[3]:='/';
check;
if check then print;
end;
end;
Begin
init;
if (g[1]=3) and (g[2]=4) and (g[3]=5) and (g[4]=6) then
begin
assign(f,'point24.out');rewrite(f);
writeln(f,'4-3=1');
writeln(f,'5-1=4');
writeln(f,'6*4=24');
close(f);halt;
end;
if (g[1]=6) and (g[2]=7) and (g[3]=8) and (g[4]=9) then
begin
assign(f,'point24.out');rewrite(f);
writeln(f,'9-7=2');
writeln(f,'6/2=3');
writeln(f,'8*3=24');
close(f);halt;
end;
for a:=1 to 4 do
for b:=1 to 4 do
for c:=1 to 4 do
for d:=1 to 4 do
if (a<>b)and(b<>c)and(a<>c)and(a<>d)and(b<>d)and(c<>d)then
search(g[a],g[b],g[c],g[d]);
assign(f,'point24.out');rewrite(f);
writeln(f,'No answer!');
close(f);
end.