Delp的代码的确有许多可资借鉴之处,佩服。借用他的一些想法,把我的代码修改如下:(如果要求四个4的话,可以将四个循环起止值都该为4啊)
procedure TForm1.Button1Click(Sender: TObject);
var i1,i2,i3,i4,s1,s2,s3:integer;
str,ch:string;
function ResultOf(i1,i2,i3,i4,s1,s2,s3:integer):integer;
var
i:integer;
begin
case s1 of
1:i:=i1+i2;
2:if i1>i2 then
i:=i1-i2
else
begin
result:=0;exit;
end;
3:i:=i1*i2;
4:if i1 mod i2 =0 then
i:=i1 div i2
else
begin
result:=0;exit;
end;
end;
case s2 of
1:i:=i+i3;
2:if i>i3 then
i:=i-i3
else
begin
result:=0;exit;
end;
3:i:=i*i3;
4:if i mod i3 =0 then
i:=i div i3
else
begin
result:=0;exit;
end;
end;
case s3 of
1:i:=i+i4;
2:if i>i4 then
i:=i-i4
else
begin
result:=0;exit;
end;
3:i:=i*i4;
4:if i mod i4 =0 then
i:=i div i4
else
begin
result:=0;exit;
end;
end;
result:=i;
end;
begin
ch:='+-*/';
for i1:=1 to 99do
for s1:=1 to 4do
for i2:=1 to 99do
for s2:=1 to 4do
for i3:=1 to 99do
for s3:=1 to 4do
for i4:=1 to 99do
begin
str:=inttostr(i1)+ch[s1]+inttostr(i2)+ch[s2]+inttostr(i3)+ch[s3]+inttostr(i4)+'=1';
edit1.Text :=str;
edit1.Update ;
if ResultOf(i1,i2,i3,i4,s1,s2,s3)=1 then
memo1.lines.add(str);
end;
end;