算法:枚举
这道题调了半天的说,RP不佳啊,错在两个地方了,一个是不等于写成了大于,一个是数组开小了……
其实这道题还是挺好做的,只要分析到位就行了。
1.先存上四个数。
2.开数组记录两个数之间的加减乘除。
3.开数组记录三个数之间的加减乘除。
4.最后的结果有两种可能:
两个数与两个数之间构成24点。
三个数与一个数构成24点。
这道题调了半天的说,RP不佳啊,错在两个地方了,一个是不等于写成了大于,一个是数组开小了……
其实这道题还是挺好做的,只要分析到位就行了。
1.先存上四个数。
2.开数组记录两个数之间的加减乘除。
3.开数组记录三个数之间的加减乘除。
4.最后的结果有两种可能:
两个数与两个数之间构成24点。
三个数与一个数构成24点。
分别枚举一下即可。
program vijos1134;
var
tot:longint;
f:array [0..4,0..4,0..6000] of real;
ff:array [0..4,0..4,0..4,0..6000] of real;
c:array [0..4,0..4] of longint;
cc:array [0..4,0..4,0..4] of longint;
a:array [0..4] of longint;
s:string;
procedure init;
var
i:longint;
begin
readln(s);
for i:=1 to length(s) do
begin
if s[i]=' ' then continue
else if s[i]='0' then continue
else if (s[i]='1') and (s[i+1]='0') then
begin
inc(tot);
a[tot]:=10;
end
else
begin
case s[i] of
'A':begin
inc(tot);
a[tot]:=1;
continue;
end;
'J':begin
inc(tot);
a[tot]:=11;
continue;
end;
'Q':begin
inc(tot);
a[tot]:=12;
continue;
end;
'K':begin
inc(tot);
a[tot]:=13;
continue;
end;
end;{case}
inc(tot);
a[tot]:=ord(s[i])-48;
end;
end;
end;
procedure main1;
var
i,j,k:longint;
begin
fillchar(f,sizeof(f),0);
for i:=1 to 4 do
begin
for j:=1 to 4 do
begin
if (i<>j) and (c[i,j]=0) then
begin
inc(c[i,j]);
f[i,j,c[i,j]]:=a[i]+a[j];
inc(c[i,j]);
f[i,j,c[i,j]]:=a[i]-a[j];
inc(c[i,j]);
f[i,j,c[i,j]]:=a[j]-a[i];
inc(c[i,j]);
f[i,j,c[i,j]]:=a[i]*a[j];
if a[j]<>0 then
begin
inc(c[i,j]);
f[i,j,c[i,j]]:=a[i]/a[j];
end;
if a[i]<>0 then
begin
inc(c[i,j]);
f[i,j,c[i,j]]:=a[j]/a[i];
end;
c[j,i]:=-1;
end;
end;
end;
end;
procedure main2;
var
i,j,k,t:longint;
begin
fillchar(cc,sizeof(cc),0);
fillchar(ff,sizeof(ff),0);
for k:=1 to 4 do
begin
for i:=1 to 4 do
begin
for j:=1 to 4 do
begin
if (i<>j) and (i<>k) and (k<>j) and (c[i,j]>0) and (cc[k,i,j]=0) then
begin
for t:=1 to c[i,j] do
begin
inc(cc[k,i,j]);
ff[k,i,j,cc[k,i,j]]:=f[i,j,t]+a[k];
inc(cc[k,i,j]);
ff[k,i,j,cc[k,i,j]]:=a[k]-f[i,j,t];
inc(cc[k,i,j]);
ff[k,i,j,cc[k,i,j]]:=f[i,j,t]-a[k];
inc(cc[k,i,j]);
ff[k,i,j,cc[k,i,j]]:=f[i,j,t]*a[k];
if a[k]<>0 then
begin
inc(cc[k,i,j]);
ff[k,i,j,cc[k,i,j]]:=f[i,j,t]/a[k];
end;
if f[i,j,t]<>0 then
begin
inc(cc[k,i,j]);
ff[k,i,j,cc[k,i,j]]:=a[k]/f[i,j,t];
end;
end;
cc[k,j,i]:=-1;
cc[i,j,k]:=-1;
cc[i,k,j]:=-1;
cc[j,i,k]:=-1;
cc[j,k,i]:=-1;
end;
end;
end;
end;
end;
procedure print1;
var
i,j,k,l,t:longint;
begin
for i:=1 to 4 do
begin
for j:=1 to 4 do
begin
for k:=1 to 4 do
begin
for l:=1 to 4 do
begin
if (j<>k) and (j<>l) and (k<>l) and (j<>i) and (k<>i) and (l<>i) and (cc[j,k,l]>0) then
begin
for t:=1 to cc[j,k,l] do
begin
if abs(a[i]+ff[j,k,l,t]-24)<0.00001 then
begin
writeln(1);
close(input);
close(output);
halt;
end;
if abs(a[i]-ff[j,k,l,t]-24)<0.00001 then
begin
writeln(1);
close(input);
close(output);
halt;
end;
if abs(ff[j,k,l,t]-a[i]-24)<0.00001 then
begin
writeln(1);
close(input);
close(output);
halt;
end;
if abs(a[i]*ff[j,k,l,t]-24)<0.00001 then
begin
writeln(1);
close(input);
close(output);
halt;
end;
if (ff[j,k,l,t]<>0) and (abs(a[i]/ff[j,k,l,t]-24)<0.00001) then
begin
writeln(1);
close(input);
close(output);
halt;
end;
if (a[i]<>0) and (abs(ff[j,k,l,t]/a[i]-24)<0.00001) then
begin
writeln(1);
close(input);
close(output);
halt;
end;
end;
end;
end;
end;
end;
end;
end;
procedure print2;
var
i,j,k,l,t1,t2:longint;
begin
for i:=1 to 4 do
begin
for j:=1 to 4 do
begin
for k:=1 to 4 do
begin
for l:=1 to 4 do
begin
if (i<>j) and (i<>k) and (i<>l) and (j<>k) and (j<>l) and (k<>l) and (c[i,j]>0) and (c[k,l]>0) then
begin
for t1:=1 to c[i,j] do
begin
for t2:=1 to c[k,l] do
begin
if abs(f[i,j,t1]+f[k,l,t2]-24)<0.00001 then
begin
writeln(1);
close(input);
close(output);
halt;
end;
if abs(f[i,j,t1]-f[k,l,t2]-24)<0.00001 then
begin
writeln(1);
close(input);
close(output);
halt;
end;
if abs(f[k,l,t2]-f[i,j,t1]-24)<0.00001 then
begin
writeln(1);
close(input);
close(output);
halt;
end;
if abs(f[i,j,t1]*f[k,l,t2]-24)<0.00001 then
begin
writeln(1);
close(input);
close(output);
halt;
end;
if (f[k,l,t2]<>0) and (abs(f[i,j,t1]/f[k,l,t2]-24)<0.00001) then
begin
writeln(1);
close(input);
close(output);
halt;
end;
if (f[i,j,t1]<>0) and (abs(f[k,l,t2]/f[i,j,t1]-24)<0.00001) then
begin
writeln(1);
close(input);
close(output);
halt;
end;
end;
end;
end;
end;
end;
end;
end;
end;
begin
assign(input,'VJ1134.in'); reset(input);
assign(output,'VJ1134.out'); rewrite(output);
init;
main1;
main2;
print1;
print2;
writeln(0);
close(input); close(output);
end.