Home > Back-end >  Combination algorithm
Combination algorithm

Time:11-11

A number n (n is not fixed, variable) a [1], a [2]... A [n], the corresponding value of the range is 0 ~ v [n], integer, algorithm, a list that n number of all possible combinations

CodePudding user response:

 
.
Type
TForm1=class (TForm)
Memo1: TMemo;
For: TButton;
Edit1: TEdit;
Procedure Button1Click (Sender: TObject);
Private
Procedure DFS (step: Integer);
Public
{Public declarations}
end;

Var
Form1: TForm1;
The data, a, b: an array of integer;
M: integer;
N: Integer;

Implementation

{$R *. DFM}

Procedure TForm1. Button1Click (Sender: TObject);
Var
I, j: Integer;
The begin
//edit1. Text:='1,2,3,4,5,6,7';
Memo1. Lines. BeginUpdate;
Memo1. The Clear;
Memo1. Lines. Delimiter:=', ';
Memo1. Lines. DelimitedText:=edit1. Text;
M:=Memo1. Lines. The Count;
SetLength (data, m + 1);
SetLength (a, m + 1);
Setlength (b, m + 1);
For I:=0 to memo1. Lines. Do the Count - 1
Data: [I + 1]=StrToInt (Memo1. Lines [I]);
Memo1. The Clear;

For I:=1 m downto do
The begin
For j:=0 to I do
The begin
A: [j]=0;
B: [j]=0;
end;
N:=I;
DFS (1);
end;
Memo1. Lines. EndUpdate;
SetLength (data, 0);
SetLength (a, 0);
SetLength (b, 0);
end;

Procedure TForm1. DFS (step: Integer);
Var
I: integer;
S: a string;
The begin
If step> N then
The begin
S:=';
For I:=1 to n do
S:=s + '+ inttostr (a [I]);

Memo1. Lines. The Add (s);
exit;
end;

For I:=1 to m do
If b [I]=0 then
The begin
A: [step]=data [I];
If a [] step - 1 & gt; A [step], then Continue;
B: [I]=1;
DFS (step + 1);
B: [I]=0;
end;
end;
.

CodePudding user response:

C is o (n, 1), C (n, 2)... C (n, n)?
  • Related