我前天做了个简单的24点的程序(绝对不是最优解,不过可以给你参考一下),见笑:
procedure TForm1.Button1Click(Sender: TObject);
var
sum : array[1..4] of integer;
fuhao :array[1..4] of byte; // '+','-','*','/' => 0,1,2,3
fh :array[0..3] of char;
i,j,k,l :integer; //sum
m,n,o :integer; //fuhao
jsjieguo :integer;
//'+' '-' '*' '/' -> 0 1 2 3
function JS(const a,b,c,d:Integer; const j1,j2,j3:byte):integer; function sf(const x,y:Integer; const jj:byte):integer;
begin
case jj of
0: sf:=x+y;
1: sf:=x-y;
2: sf:=x*y;
3: if y<>0 then
if x = ((x div y)*y) then sf := (x div y) else sf := 9991
else sf := 9991 ;
end;
end; begin
JS := sf( sf( sf(a,b,j1),c,j2 ), d,j3);// (a j1 b j2 c j3 d)
end;
begin
sum[1]:=strToint(Edit1.Text);
sum[2]:=strToint(Edit2.Text);
sum[3]:=strToint(Edit3.Text);
sum[4]:=strToint(Edit4.Text);
ListBox1.Items.Add('====='+Edit1.Text + ' ' + Edit2.Text + ' ' +
Edit3.Text + ' ' + Edit4.Text+'=====');
ListBox1.TopIndex :=ListBox1.Items.count-1;
fuhao[1]:=0; fuhao[2]:=1; fuhao[3]:=2; fuhao[4]:=3; fh[0]:='+'; fh[1]:='-'; fh[2]:='*'; fh[3]:='/'; for i:=1 to 4 do
for j:=1 to 4 do
if (i<>j) then
for k:=1 to 4 do
if(i<>k) and (j<>k) then
for l:=1 to 4 do
if(i<>l) and (j<>l) and (k<>l) then
begin
for m:=1 to 4 do
for n:=1 to 4 do
// if(m<>n) then
for o:=1 to 4 do
// if (m<>o) and (n<>o) then
begin
jsjieguo := JS(sum[i],sum[j],sum[k],sum[l],fuhao[m],fuhao[n], fuhao[o]);
if (jsjieguo<1000) and (Jsjieguo=24) then
begin
Label1.Caption:=intTostr(jsjieguo);
ListBox1.Items.Add('((('+
intTostr(sum[i])+fh[fuhao[m]]+intTostr(sum[j])+')'+
fh[fuhao[n]]+intTostr(sum[k])+')'+
fh[fuhao[o]]+intTostr(sum[l])+')'+
'='+intTostr(jsjieguo));
ListBox1.TopIndex :=ListBox1.Items.count-1;
end;
end;
end;
Label2.Caption := 'End';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
sum : array[1..4] of integer;
fuhao :array[1..4] of byte; // '+','-','*','/' => 0,1,2,3
fh :array[0..3] of char;
i,j,k,l :integer; //sum
m,n,o :integer; //fuhao
jsjieguo :integer;
//'+' '-' '*' '/' -> 0 1 2 3
function JS(const a,b,c,d:Integer; const j1,j2,j3:byte):integer; function sf(const x,y:Integer; const jj:byte):integer;
begin
case jj of
0: sf:=x+y;
1: sf:=x-y;
2: sf:=x*y;
3: if y<>0 then
if x = ((x div y)*y) then sf := (x div y) else sf := 9991
else sf := 9991 ;
end;
end; begin
JS := sf( sf( sf(a,b,j1),c,j2 ), d,j3);// (a j1 b j2 c j3 d)
end;
begin
sum[1]:=strToint(Edit1.Text);
sum[2]:=strToint(Edit2.Text);
sum[3]:=strToint(Edit3.Text);
sum[4]:=strToint(Edit4.Text);
ListBox1.Items.Add('====='+Edit1.Text + ' ' + Edit2.Text + ' ' +
Edit3.Text + ' ' + Edit4.Text+'=====');
ListBox1.TopIndex :=ListBox1.Items.count-1;
fuhao[1]:=0; fuhao[2]:=1; fuhao[3]:=2; fuhao[4]:=3; fh[0]:='+'; fh[1]:='-'; fh[2]:='*'; fh[3]:='/'; for i:=1 to 4 do
for j:=1 to 4 do
if (i<>j) then
for k:=1 to 4 do
if(i<>k) and (j<>k) then
for l:=1 to 4 do
if(i<>l) and (j<>l) and (k<>l) then
begin
for m:=1 to 4 do
for n:=1 to 4 do
// if(m<>n) then
for o:=1 to 4 do
// if (m<>o) and (n<>o) then
begin
jsjieguo := JS(sum[i],sum[j],sum[k],sum[l],fuhao[m],fuhao[n], fuhao[o]);
if (jsjieguo<1000) and (Jsjieguo=24) then
begin
Label1.Caption:=intTostr(jsjieguo);
ListBox1.Items.Add('((('+
intTostr(sum[i])+fh[fuhao[m]]+intTostr(sum[j])+')'+
fh[fuhao[n]]+intTostr(sum[k])+')'+
fh[fuhao[o]]+intTostr(sum[l])+')'+
'='+intTostr(jsjieguo));
ListBox1.TopIndex :=ListBox1.Items.count-1;
end;
end;
end;
Label2.Caption := 'End';
end;
(((a j1 b) j2 c) j3 d)其中a、b、c、d是整数j1、j2、j3为运算符号!你只要在窗体上加4个EDIT控件、1个ListBox控件、1个Label、1个Button。要考虑括号和运算符号的优先级用二叉树就可以了,自己试吧。