穷举表达式(100分)

  • 主题发起人 主题发起人 kmask
  • 开始时间 开始时间
K

kmask

Unregistered / Unconfirmed
GUEST, unregistred user!
原贴在此
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2323106
请大家前往关注
原贴100,现再加100,请大侠出手
万谢
 
不就是稍稍修改一下嘛...

事先将各个变量放在Memo1中——一个变量一行。
type
SAA=array of array of String;
TOutputFunc=procedure (Text:String);
TOpts=(opAdd,opMinus,opMul,opDiv);
const
Operators:array[TOpts]of String=('+','-','*','/');
function EnumStrExpr(Strs:array of String;OutFunc:TOutputFunc):Integer;
var
N:Integer;
Opts:array of TOpts;
procedure ShowResult(Level:Integer);
var
i:Integer;
Str:String;
begin
if @OutFunc=nil then
exit;
Str:='';
for i:=0 to Leveldo
begin
Str:=Str+Strs;
if i<Level then
Str:=Str+Operators[Opts];
end;
OutFunc(Str);
end;
proceduredo
After(Level:Integer);
var
j:TOpts;
begin
if Level=N-1 then
begin
Inc(Result);
ShowResult(Level);
end
else
begin
for j:=Low(TOpts) to High(TOpts)do
begin
Opts[Level]:=j;
do
After(Level+1);
end;
end;
end;
begin
Result:=0;
N:=High(Strs)+1;
SetLength(Opts,N);
do
After(0);
SetLength(Opts,0);
end;

function PNNNum(N:Integer):Integer;
var
i:Integer;
begin
Result:=1;
for i:=2 to Ndo
Result:=Result*i;
end;
function PNNStr(Strs:array of String):SAA;
var
A:array of Integer;
i,N,TotalCount,C:Integer;
procedure Gen(Level:Integer);
var
j,mm:Integer;
begin
if Level=N then
begin
SetLength(Result[c],N);
for j:=0 to N-1do
Result[c][j]:=Strs[A[j]];
Inc(c);
exit;
end;
mm:=A[Level];
for j:=Level to N-1do
begin
A[Level]:=A[j];
A[j]:=mm;
Gen(Level+1);
A[j]:=A[Level];
A[Level]:=mm;
end;
end;
begin
N:=High(Strs)+1;
TotalCount:=PNNNum(N);
SetLength(Result,TotalCount);
SetLength(A,N);
for i:=0 to N-1do
A:=i;
c:=0;
Gen(0);
SetLength(A,0);
end;

procedure Output(Text:String);
begin
Form1.Memo2.Lines.Add(Text);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Strs:array of String;
EnumStrs:SAA;
i,c:Integer;
begin
SetLength(Strs,Memo1.Lines.Count);
for i:=0 to High(Strs)do
Strs:=Memo1.Lines;
c:=0;
Memo2.Clear;
Memo2.Lines.begin
Update;
if not CheckBox1.Checked then
begin
EnumStrs:=PNNStr(Strs);
for i:=Low(EnumStrs) to High(EnumStrs)do
Inc(c,EnumStrExpr(EnumStrs,@Output));
end
else
c:=EnumStrExpr(Strs,@Output);
Memo2.Lines.EndUpdate;
Memo2.Lines.Add('TotalCount: '+IntToStr(c));
end;
 
接受答案了.
 
后退
顶部