用递归啊:
下面是我的一段程序,可以参考一下:
是我用来模拟彩票几选几做的,列出所有组合。
其中变量Y就是你的m,Z是就是你的n。
我是初学者,各位大侠指教,批评~~
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
procedure GetResult;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
A: array[1..2]of array of integer;
s: string;
X,Y,Z: integer;
implementation
{$R *.dfm}
procedure TForm1.GetResult;
var
i:integer;
begin
for i:=0 to Y-1do
begin
if A[2]=1 then
continue else
begin
s:=s+' '+inttostr(A[1]+1);
X:=X+1;
A[2]:=1;
if X=Z then
begin
Memo1.Lines.Add(s);
if i>9 then
s:=copy(s,1,length(s)-3) else
s:=copy(s,1,length(s)-2);
X:=X-1;
A[2]:=0;
end
else
begin
GetResult;
if i>9 then
s:=copy(s,1,length(s)-3) else
s:=copy(s,1,length(s)-2);
X:=X-1;
A[2]:=0;
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
Memo1.Clear;
Y:=strtoInt(edit1.text);
Z:=strtoint(edit2.Text);
setlength(A[1],Y);
setlength(A[2],Y);
X:=0;
if (Y<Z)or(Y>99) then
raise exception.Create('输入错误');
for i:=0 to Y-1do
begin
A[1]:=i;
A[2]:=0;
end;
getresult;
showmessage('共 '+inttostr(memo1.Lines.Count)+ '种组合');
end;
end.