“加.减.乘.除, 1 .2 .3 .4 .5 ” 全都要用,而且不可以重复。
zswangII(伴水清清)(职业清洁工)帮我解决了排列组合算法的问题,结果共得到
2881种可能的组合,就是没有值22。大家看一下,是不是得数22是不可能的??
我写得求值原代码如下(感谢zswangII(伴水清清)(职业清洁工)提供算法函数):
function Collocate(mStrings: TStrings; mStr: string): Boolean; { 全排列 }
procedure pCollocate(mLeft, mRight: string);
var
I, L: Integer;
Temp: string;
begin
L := Length(mLeft);
if L = 0 then
mStrings.Add(mRight)
else for I := 1 to L do begin
Temp := mLeft;
Delete(Temp, I, 1);
pCollocate(Temp, Concat(mRight, mLeft[I]));
end;
end;
begin
Result := False;
if not Assigned(mStrings) then Exit;
mStrings.BeginUpdate;
try
mStrings.Clear;
pCollocate(mStr, '');
finally
mStrings.EndUpdate;
end;
Result := True;
end; { Collocate }
procedure TForm1.Button2Click(Sender: TObject);
var
rd,rs:tstrings;
i,j:integer;
function calc(const dd,ss:string):boolean;
var
rs:integer;
rr:string;
ad:array[0..4] of integer;
ac:array[0..3] of char;
begin
result:=false;
ad[0]:=strtoint(dd[1]);ad[1]:=strtoint(dd[2]);ad[2]:=strtoint(dd[3]);
ad[3]:=strtoint(dd[4]);ad[4]:=strtoint(dd[5]);
case strtoint(ss[1]) of
1:ac[0]:='+';
2:ac[0]:='-';
3:ac[0]:='*';
4:ac[0]:='/';
end;
case strtoint(ss[2]) of
1:ac[1]:='+';
2:ac[1]:='-';
3:ac[1]:='*';
4:ac[1]:='/';
end;
case strtoint(ss[3]) of
1:ac[2]:='+';
2:ac[2]:='-';
3:ac[2]:='*';
4:ac[2]:='/';
end;
case strtoint(ss[4]) of
1:ac[3]:='+';
2:ac[3]:='-';
3:ac[3]:='*';
4:ac[3]:='/';
end;
rr:=inttostr(ad[0])+ac[0]+
inttostr(ad[1])+ac[1]+
inttostr(ad[2])+ac[2]+
inttostr(ad[3])+ac[3]+inttostr(ad[4]);
try
case ac[0] of
'+':rs:=ad[0]+ad[1];
'-':rs:=ad[0]-ad[1];
'*':rs:=ad[0]*ad[1];
'/':rs:=ad[0] div ad[1];
end;//case
case ac[1] of
'+':rs:=rs+ad[2];
'-':rs:=rs-ad[2];
'*':rs:=rs*ad[2];
'/':rs:=rs div ad[2];
end;//case
case ac[2] of
'+':rs:=rs+ad[3];
'-':rs:=rs-ad[3];
'*':rs:=rs*ad[3];
'/':rs:=rs div ad[3];
end;//case
case ac[3] of
'+':rs:=rs+ad[4];
'-':rs:=rs-ad[4];
'*':rs:=rs*ad[4];
'/':rs:=rs div ad[4];
end;//case if rs=22 then
begin
label1.Caption:=rr;
result:=true;
end;
finally
memo1.Lines.Add(rr+'----'+inttostr(rs));
end;//try
end;
begin
rd:=tstringlist.Create;
rs:=tstringlist.Create;
Collocate(rd, '12345');
Collocate(rs, '1234');
for i:=0 to rd.Count-1 do
begin
for j:=0 to rs.Count-1 do
begin
calc(rd.Strings[i],rs.Strings[j]);
end;//for j:=0 to rs.Count-1 do
end;//for
label2.Caption:=inttostr(memo1.Lines.Count);
rd.Free;
rs.Free;
end;
zswangII(伴水清清)(职业清洁工)帮我解决了排列组合算法的问题,结果共得到
2881种可能的组合,就是没有值22。大家看一下,是不是得数22是不可能的??
我写得求值原代码如下(感谢zswangII(伴水清清)(职业清洁工)提供算法函数):
function Collocate(mStrings: TStrings; mStr: string): Boolean; { 全排列 }
procedure pCollocate(mLeft, mRight: string);
var
I, L: Integer;
Temp: string;
begin
L := Length(mLeft);
if L = 0 then
mStrings.Add(mRight)
else for I := 1 to L do begin
Temp := mLeft;
Delete(Temp, I, 1);
pCollocate(Temp, Concat(mRight, mLeft[I]));
end;
end;
begin
Result := False;
if not Assigned(mStrings) then Exit;
mStrings.BeginUpdate;
try
mStrings.Clear;
pCollocate(mStr, '');
finally
mStrings.EndUpdate;
end;
Result := True;
end; { Collocate }
procedure TForm1.Button2Click(Sender: TObject);
var
rd,rs:tstrings;
i,j:integer;
function calc(const dd,ss:string):boolean;
var
rs:integer;
rr:string;
ad:array[0..4] of integer;
ac:array[0..3] of char;
begin
result:=false;
ad[0]:=strtoint(dd[1]);ad[1]:=strtoint(dd[2]);ad[2]:=strtoint(dd[3]);
ad[3]:=strtoint(dd[4]);ad[4]:=strtoint(dd[5]);
case strtoint(ss[1]) of
1:ac[0]:='+';
2:ac[0]:='-';
3:ac[0]:='*';
4:ac[0]:='/';
end;
case strtoint(ss[2]) of
1:ac[1]:='+';
2:ac[1]:='-';
3:ac[1]:='*';
4:ac[1]:='/';
end;
case strtoint(ss[3]) of
1:ac[2]:='+';
2:ac[2]:='-';
3:ac[2]:='*';
4:ac[2]:='/';
end;
case strtoint(ss[4]) of
1:ac[3]:='+';
2:ac[3]:='-';
3:ac[3]:='*';
4:ac[3]:='/';
end;
rr:=inttostr(ad[0])+ac[0]+
inttostr(ad[1])+ac[1]+
inttostr(ad[2])+ac[2]+
inttostr(ad[3])+ac[3]+inttostr(ad[4]);
try
case ac[0] of
'+':rs:=ad[0]+ad[1];
'-':rs:=ad[0]-ad[1];
'*':rs:=ad[0]*ad[1];
'/':rs:=ad[0] div ad[1];
end;//case
case ac[1] of
'+':rs:=rs+ad[2];
'-':rs:=rs-ad[2];
'*':rs:=rs*ad[2];
'/':rs:=rs div ad[2];
end;//case
case ac[2] of
'+':rs:=rs+ad[3];
'-':rs:=rs-ad[3];
'*':rs:=rs*ad[3];
'/':rs:=rs div ad[3];
end;//case
case ac[3] of
'+':rs:=rs+ad[4];
'-':rs:=rs-ad[4];
'*':rs:=rs*ad[4];
'/':rs:=rs div ad[4];
end;//case if rs=22 then
begin
label1.Caption:=rr;
result:=true;
end;
finally
memo1.Lines.Add(rr+'----'+inttostr(rs));
end;//try
end;
begin
rd:=tstringlist.Create;
rs:=tstringlist.Create;
Collocate(rd, '12345');
Collocate(rs, '1234');
for i:=0 to rd.Count-1 do
begin
for j:=0 to rs.Count-1 do
begin
calc(rd.Strings[i],rs.Strings[j]);
end;//for j:=0 to rs.Count-1 do
end;//for
label2.Caption:=inttostr(memo1.Lines.Count);
rd.Free;
rs.Free;
end;
Memo1
1+2-3*4/5----0
1+2-3/4*5----0
1+2*3-4/5----1
1+2*3/4-5-----3
1+2/3-4*5-----15
1+2/3*4-5-----1
1-2+3*4/5----1
1-2+3/4*5----0
1-2*3+4/5----0
1-2*3/4+5----5
1-2/3+4*5----20
1-2/3*4+5----5
1*2+3-4/5----0
1*2+3/4-5-----4
1*2-3+4/5----0
1*2-3/4+5----5
1*2/3+4-5-----1
1*2/3-4+5----1
1/2+3-4*5-----5
1/2+3*4-5----7
1/2-3+4*5----5
1/2-3*4+5-----7
1/2*3+4-5-----1
1/2*3-4+5----1
1+2-3*5/4----0
1+2-3/5*4----0
1+2*3-5/4----1
1+2*3/5-4-----3
1+2/3-5*4-----16
1+2/3*5-4----1
1-2+3*5/4----2
1-2+3/5*4----0
1-2*3+5/4----0
1-2*3/5+4----4
...........................................太长了,不能粘贴完。
((5-1)*((3/2)+4))
((((3/2)-1)+5)*4)
(((5-1)+(3/2))*4)
((5-1)*((3/2)+4))
((((3/2)+5)-1)*4)
((5-1)*((3/2)+4))var
vNumbers: array[0..4] of Extended;
vExpressions: array[0..4] of string;
vOutputStrings: TStrings;const
cPrecision = 1E-6;
cDest = 22;procedure Init(mStrings: TStrings);
var
I: Integer;
begin
vOutputStrings := mStrings;
vOutputStrings.Clear;
for I := 0 to 4 do begin
vNumbers[I] := I + 1;
vExpressions[I] := IntToStr(I + 1);
end;
end;procedure SearchExpression(mLevel: Integer; mOperators: string);
var
I, J: Integer;
A, B: Extended;
vExpA, vExpB: string;
begin
if (mLevel <= 1) and (Abs(vNumbers[0] - cDest) <= cPrecision) then begin
vOutputStrings.Add(vExpressions[0]);
Exit;
end; for I := 0 to mLevel - 1 do begin
for J := I + 1 to mLevel - 1 do begin
A := vNumbers[I];
B := vNumbers[J];
vNumbers[J] := vNumbers[mLevel - 1]; vExpA := vExpressions[I];
vExpB := vExpressions[J];
vExpressions[J] := vExpressions[mLevel - 1]; if Pos('+', mOperators) <= 0 then begin
vExpressions[I] := '(' + vExpA + '+' + vExpB + ')';
vNumbers[I] := A + B;
SearchExpression(mLevel - 1, mOperators + '+');
end;
if Pos('-', mOperators) <= 0 then begin
vExpressions[I] := '(' + vExpA + '-' + vExpB + ')';
vNumbers[I] := A - B;
SearchExpression(mLevel - 1, mOperators + '-');
vExpressions[I] := '(' + vExpB + '-' + vExpA + ')';
vNumbers[I] := B - A;
SearchExpression(mLevel - 1, mOperators + '-');
end;
if Pos('*', mOperators) <= 0 then begin
vExpressions[I] := '(' + vExpA + '*' + vExpB + ')';
vNumbers[I] := A * B;
SearchExpression(mLevel - 1, mOperators + '*');
end;
if Pos('/', mOperators) <= 0 then begin
if B <> 0 then begin
vExpressions[I] := '(' + vExpA + '/' + vExpB + ')';
vNumbers[I] := A / B;
SearchExpression(mLevel - 1, mOperators + '/');
end;
if A <> 0 then begin
vExpressions[I] := '(' + vExpB + '/' + vExpA + ')';
vNumbers[I] := B / A;
SearchExpression(mLevel - 1, mOperators + '/');
end;
end;
vNumbers[I] := A;
vNumbers[J] := B;
vExpressions[I] := vExpA;
vExpressions[J] := vExpB;
end;
end;
end;procedure Calc;
begin
vOutputStrings.BeginUpdate;
try
SearchExpression(5, '');
finally
vOutputStrings.EndUpdate;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Init(Memo1.Lines);
Calc;
end;
真是高手啊,我花了一个下午也没搞定,他10分钟不到就写出原代码、求出结果来了。