在大富翁上看到的!谁能解释一下 排列组合(递归法) ( 积分: 0 )

  • 主题发起人 主题发起人 infernor
  • 开始时间 开始时间
I

infernor

Unregistered / Unconfirmed
GUEST, unregistred user!
在大富翁上看到的!谁能解释一下

排列组合(递归法)
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;

type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
//a : Array[0..1024] of Integer;
a : Array of Integer;

implementation

{$R *.dfm}

procedure Combination(AM, AK : Integer; Out Asl : TStringList);
Var
II, JJ : Integer;
s : String;
begin
for II := AM DownTo AK Do
Begin
a[AK] := II;
If AK>1 Then Combination(II-1, AK-1, Asl)
Else
Begin
s := '';
For JJ := a[0] Downto 1 Do
If JJ = 1 Then s := s + IntToStr(a[JJ]) Else s := s + IntToStr(a[JJ]) + ' , ';
Asl.Add(s);
End;
End;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
Var
sl : TStringList;
i : Integer;
Num, Den : Longint;
Sup, Sub : Integer; //下标、上标
begin
Memo1.Clear;
sl := TStringList.Create;
sl.Clear;
//相当于C(10, 6) 10为下标,6为上标 (上、下标怎么表示??)
Sup := StrToInt(Edit1.Text); //下标 10 Edit1.Text = '10';
Sub := StrToInt(Edit2.Text); //上标 6 Edit2.Text = '6';
Num := 1; Den := 1;
For i := Sub Downto 1 Do
begin
Num := Num * (Sup - i + 1);
Den := Den * i;
end;
SetLength(a, Trunc(Num/Den));
a[0] := Sub;
//
Combination(Sup, Sub, sl);
for i:=0 To (sl.Count-1) Do
Memo1.Lines.Add('【' + FormatFloat('00000000', i+1) + '】 ' + sl.Strings);
FreeAndNil(sl);
end;

end.
 
在大富翁上看到的!谁能解释一下

排列组合(递归法)
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;

type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
//a : Array[0..1024] of Integer;
a : Array of Integer;

implementation

{$R *.dfm}

procedure Combination(AM, AK : Integer; Out Asl : TStringList);
Var
II, JJ : Integer;
s : String;
begin
for II := AM DownTo AK Do
Begin
a[AK] := II;
If AK>1 Then Combination(II-1, AK-1, Asl)
Else
Begin
s := '';
For JJ := a[0] Downto 1 Do
If JJ = 1 Then s := s + IntToStr(a[JJ]) Else s := s + IntToStr(a[JJ]) + ' , ';
Asl.Add(s);
End;
End;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
Var
sl : TStringList;
i : Integer;
Num, Den : Longint;
Sup, Sub : Integer; //下标、上标
begin
Memo1.Clear;
sl := TStringList.Create;
sl.Clear;
//相当于C(10, 6) 10为下标,6为上标 (上、下标怎么表示??)
Sup := StrToInt(Edit1.Text); //下标 10 Edit1.Text = '10';
Sub := StrToInt(Edit2.Text); //上标 6 Edit2.Text = '6';
Num := 1; Den := 1;
For i := Sub Downto 1 Do
begin
Num := Num * (Sup - i + 1);
Den := Den * i;
end;
SetLength(a, Trunc(Num/Den));
a[0] := Sub;
//
Combination(Sup, Sub, sl);
for i:=0 To (sl.Count-1) Do
Memo1.Lines.Add('【' + FormatFloat('00000000', i+1) + '】 ' + sl.Strings);
FreeAndNil(sl);
end;

end.
 

Similar threads

后退
顶部