【程序】用free pascal写了一个24点暴力计算器
24点吧
全部回复
仅看楼主
level 1
ax_pokl 楼主
const n=132;
const plus=%0001;
const muls=%0010;
const mins=%0100;
const divs=%1000;
var a1,a2,a3,a4:integer;
var a,b,c,k:array[1..4]of integer;
var i1,i2,i3,i4,i5,i6,i7,i8,i9,i0:integer;
var j1,j2,j3,j4,j5,j6,j7,j8,j9,j0:integer;
var k1,k2,k3,k4,k5,k6,k7,k8,k9,k0:integer;
var m1,m2,m3,m4,m5,m6,m7,m8,m9,m0:integer;
var can,next:boolean;
var sum,sum1,sum2:real;
procedure outk(k:integer);
begin
if k=1 then write('+');
if k=2 then write('*');
if k=3 then write('-');
if k=4 then write('/');
end;
procedure outi(i:integer);
begin
write(i);
end;
procedure outp(i:integer);
begin
if i<=9 then write(i);
if i=10 then write('0');
if i=11 then write('J');
if i=12 then write('Q');
if i=13 then write('K');
end;
procedure test(flag:integer);
begin
for j1:=1 to 4 do
for j2:=1 to 4 do
for j3:=1 to 4 do
for j4:=1 to 4 do
begin
if (j1<>j2) and (j1<>j3) and (j1<>j4)
and (j2<>j3) and (j2<>j4) and (j3<>j4)
and not(can) then
begin
b[j1]:=a[1];
b[j2]:=a[2];
b[j3]:=a[3];
b[j4]:=a[4];
for k1:=1 to 4 do
for k2:=1 to 4 do
for k3:=1 to 4 do
begin
next:=false;
if can then next:=true;
if not(next) then
begin
if (k1=1) and ((flag and plus)=0) then next:=true;
if (k2=1) and ((flag and plus)=0) then next:=true;
if (k3=1) and ((flag and plus)=0) then next:=true;
if (k1=2) and ((flag and muls)=0) then next:=true;
if (k2=2) and ((flag and muls)=0) then next:=true;
if (k3=2) and ((flag and muls)=0) then next:=true;
if (k1=3) and ((flag and mins)=0) then next:=true;
if (k2=3) and ((flag and mins)=0) then next:=true;
if (k3=3) and ((flag and mins)=0) then next:=true;
if (k1=4) and ((flag and divs)=0) then next:=true;
if (k2=4) and ((flag and divs)=0) then next:=true;
if (k3=4) and ((flag and divs)=0) then next:=true;
end;
if can then next:=true;
if not(next) then
begin
sum:=0;
if k1=1 then sum:=b[1]+b[2];
if k1=2 then sum:=b[1]*b[2];
if k1=3 then sum:=b[1]-b[2];
if k1=4 then sum:=b[1]/b[2];
if k2=1 then sum:=sum+b[3];
if k2=2 then sum:=sum*b[3];
if k2=3 then sum:=sum-b[3];
if k2=4 then sum:=sum/b[3];
if k3=1 then sum:=sum+b[4];
if k3=2 then sum:=sum*b[4];
if k3=3 then sum:=sum-b[4];
if k3=4 then sum:=sum/b[4];
end;
if (abs(sum-24)<0.0001) and not(can) then
begin
can:=true;
k[1]:=k1;
k[2]:=k2;
k[3]:=k3;
c[1]:=b[1];
c[2]:=b[2];
c[3]:=b[3];
c[4]:=b[4];
i9:=1;
end;
if can then next:=true;
if not(next) then
begin
sum:=0;
sum1:=0;
sum2:=0;
if k1=1 then sum1:=b[1]+b[2];
if k1=2 then sum1:=b[1]*b[2];
if k1=3 then sum1:=b[1]-b[2];
if k1=4 then sum1:=b[1]/b[2];
if k3=1 then sum2:=b[3]+b[4];
if k3=2 then sum2:=b[3]*b[4];
if k3=3 then sum2:=b[3]-b[4];
if k3=4 then sum2:=b[3]/b[4];
if k2=1 then sum:=sum1+sum2;
if k2=2 then sum:=sum1*sum2;
if k2=3 then sum:=sum1-sum2;
if (k3=3) and (b[3]=b[4]) then sum:=0 else
if k2=4 then sum:=sum1/sum2;
end;
if (abs(sum-24)<0.0001) and not(can) then
begin
can:=true;
k[1]:=k1;
k[2]:=k2;
k[3]:=k3;
c[1]:=b[1];
c[2]:=b[2];
c[3]:=b[3];
c[4]:=b[4];
i9:=2;
end;
if can then next:=true;
if not(next) then
begin
sum:=0;
sum1:=0;
sum2:=0;
if k3=1 then sum1:=b[3]+b[4];
if k3=2 then sum1:=b[3]*b[4];
if k3=3 then sum1:=b[3]-b[4];
if k3=4 then sum1:=b[3]/b[4];
if k2=1 then sum2:=b[2]+sum1;
if k2=2 then sum2:=b[2]*sum1;
if k2=3 then sum2:=b[2]-sum1;
if (k3=3) and (b[3]=b[4]) then sum:=0 else
if k2=4 then sum2:=b[2]/sum1;
if k1=1 then sum:=b[1]+sum2;
if k1=2 then sum:=b[1]*sum2;
if k1=3 then sum:=b[1]-sum2;
if (abs(sum2)<0.0001) then sum:=0 else
if k1=4 then sum:=b[1]/sum2;
end;
if (abs(sum-24)<0.0001) and not(can) then
begin
can:=true;
k[1]:=k1;
k[2]:=k2;
k[3]:=k3;
c[1]:=b[1];
c[2]:=b[2];
c[3]:=b[3];
c[4]:=b[4];
i9:=3;
end;
if can then next:=true;
if not(next) then
begin
sum:=0;
sum1:=0;
sum2:=0;
if k2=1 then sum1:=b[2]+b[3];
if k2=2 then sum1:=b[2]*b[3];
if k2=3 then sum1:=b[2]-b[3];
if k2=4 then sum1:=b[2]/b[3];
if k3=1 then sum2:=sum1+b[4];
if k3=2 then sum2:=sum1*b[4];
if k3=3 then sum2:=sum1-b[4];
if (k2=3) and (b[2]=b[3]) then sum:=0 else
if k3=4 then sum2:=sum1/b[4];
if k1=1 then sum:=b[1]+sum2;
if k1=2 then sum:=b[1]*sum2;
if k1=3 then sum:=b[1]-sum2;
if (abs(sum2)<0.0001) then sum:=0 else
if k1=4 then sum:=b[1]/sum2;
end;
if (abs(sum-24)<0.0001) and not(can) then
begin
can:=true;
k[1]:=k1;
k[2]:=k2;
k[3]:=k3;
c[1]:=b[1];
c[2]:=b[2];
c[3]:=b[3];
c[4]:=b[4];
i9:=4;
end;
end;
end;
end;
if can then
begin
write(#9);
if (flag and plus)<>0 then write('+') else write(' ');
if (flag and muls)<>0 then write('*') else write(' ');
if (flag and mins)<>0 then write('-') else write(' ');
if (flag and divs)<>0 then write('/') else write(' ');
if (flag and plus)<>0 then m1:=m1+1;
if (flag and muls)<>0 then m2:=m2+1;
if (flag and mins)<>0 then m3:=m3+1;
if (flag and divs)<>0 then m4:=m4+1;
m0:=m0+1;
write(#9);
write('=');
if i9=1 then
begin
write('[');
write('(');
outi(c[1]);
outk(k[1]);
outi(c[2]);
write(')');
outk(k[2]);
outi(c[3]);
write(']');
outk(k[3]);
outi(c[4]);
end;
if i9=2 then
begin
write('(');
outi(c[1]);
outk(k[1]);
outi(c[2]);
write(')');
outk(k[2]);
write('(');
outi(c[3]);
outk(k[3]);
outi(c[4]);
write(')');
end;
if i9=3 then
begin
outi(c[1]);
outk(k[1]);
write('[');
outi(c[2]);
outk(k[2]);
write('(');
outi(c[3]);
outk(k[3]);
outi(c[4]);
write(')');
write(']');
end;
if i9=4 then
begin
outi(c[1]);
outk(k[1]);
write('[');
write('(');
outi(c[2]);
outk(k[2]);
outi(c[3]);
write(')');
outk(k[3]);
outi(c[4]);
write(']');
end;
end;
end;
begin
assign(output,'24dian.txt');
rewrite(output);
for a1:=1 to n do
for a2:=a1 to n do
for a3:=a2 to n do
for a4:=a3 to n do
begin
i0:=i0+1;
can:=false;
a[1]:=a1;
a[2]:=a2;
a[3]:=a3;
a[4]:=a4;
for i1:=1 to 4 do
outp(a[i1]);
for i2:=1 to 15 do
if not(can) then test(i2);
if not(can) then write(#9,'.');
writeln();
end;
writeln(i0:10,m0:10);
writeln(m1:10,m2:10,m3:10,m4:10);
close(output);
end.
2014年01月22日 22点01分 1
level 9
不明觉厉
2014年01月23日 04点01分 2
level 1
真差劲,不动脑子的做法
2014年08月08日 04点08分 3
level 6
你这个结果得和国际标准的4数网比一比
http://www.4shu.net/solutions
人家真的NB
2014年08月08日 13点08分 5
level 6
看不懂[真棒]
2014年08月09日 00点08分 6
吧务
level 9
看不懂
但支持一下
我之前用C++也写了一个,你可以往前找找
2014年08月09日 14点08分 7
level 1
24点的公约数一共有8个,
24点运算一共有4个数字,
用4选1 4选2 4选4 选择方法,每分1次 分2部分求24点,
并分2个条件,
1种符合24点的公约数,则另一部分的目标数字是24除以公约数的数字
另一部分为3个数字 分2种情况
有数字为目标数字的的公约数
没有数字为目标数字的公约数
再次筛选
另一种不符合24点的公约数,则由四则运算求出目标值
其中24点求法有 加 减 乘 除 注意1和0的情况
列出程序,筛选过程时要保存好IF条件判断出的值,并作出循环,循环的次数,符合组合的筛选次数:高三数学
随时打印出每一布,以方便下一进程的列出方程
2014年08月13日 16点08分 8
1