求所有组合--请帮忙!! (100分)

  • 主题发起人 主题发起人 Tony_comm
  • 开始时间 开始时间
T

Tony_comm

Unregistered / Unconfirmed
GUEST, unregistred user!
如何列出从a,b,c,d (或更多,有限集合 n )4个元素中取出3(m个元素 m<=n)个元素的组合列表.
我想写一个函数,给出n,m 能够返回要求的组合列表.
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1008308
 
嵌套循环
 
啊,为什么没有人说的详细些呢?
 
呵呵,数据结构版的已答问题中有很多类似问题的详细解答。
 
LeeChange,您终于看到我了,说实话前面的帖子我都看了,还是没明白,怎么办。
 
期待中....
 
哪个帖子不明白呀,偶们来探讨探讨。
 
用递归啊:
下面是我的一段程序,可以参考一下:
是我用来模拟彩票几选几做的,列出所有组合。
其中变量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.
 
LeeChange、ahjie 先谢二位了,待我仔细看来,有问题我请教。
*第一次在贵坛发贴,没想到热心富翁很多。太感谢了!!
www.delphibbs.com是个不错的地方。[:D]
 
我不知道从哪个地方提问
 
后退
顶部