http://www.delphibbs.com/delphibbs/dispq.asp?lid=0938125
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1135640
自己看吧。
鉴于大多数人通常不理会链接...贴!
//TBuffer类的代码见 http://www.delphibbs.com/delphibbs/dispq.asp?lid=0968511
function CNMNum(N,M:Integer)
ouble;
var
i:Integer;
begin
Result:=1;
for i:=N-M+1 to Ndo
Result:=Result*i;
for i:=2 to Mdo
Result:=Result/i;
end;
function CNM(const N,M:Integer;out Count:Integer):String;
var
A,R:array of Integer;
i:Integer;
Buf:TBuffer;
StartTime
Word;
TotalCount
ouble;
procedure Gen(Level:Integer);
var
j,s:Integer;
mstr:String;
begin
if Level=M then
begin
mstr:='';
for j:=0 to M-1do
mstr:=mstr+IntToStr(R[j])+#9;
mstr:=mstr+#13#10;
Buf.WriteBuf(@mstr[1],Length(mstr));
Inc(Count);
if Count mod 1000=0 then
begin
Form1.Caption:=Format('%.3f%% %.7d Time: %.2fs Buffer Size: %.2fM',
[100*Count/TotalCount,Count,(GetTickCount-StartTime)/1000,
Buf.ContentSize/(1024*1024)]);
Application.ProcessMessages;
end;
exit;
end;
if Level=0 then
s:=0
else
s:=R[Level-1]+1;
for j:=s to N-M+Leveldo
begin
R[Level]:=j;
Gen(Level+1);
end;
end;
begin
Result:='';
Count:=0;
if (N<1) or (M>N) or (M<1) then
exit;
Buf:=TBuffer.Create(0,1024*1024);
SetLength(A,N);
SetLength(R,M);
TotalCount:=CNMNum(N,M);
StartTime:=GetTickCount;
for i:=0 to N-1do
A
:=i;
Gen(0);
Result:=Buf.AsString;
Buf.Free;
end;