Home
Forum: "Main";
Current archive: 2002.01.08;

Down

#### Rather a mathematical question ... Find similar branches

Дмитрий Д.   (2001-12-17 13:28) [0]

Does anyone tell me the algorithm for obtaining all possible values ​​composed of array elements (for example, 3x3):

qwe
asd
zxc

that is, result may be for the first line:
qwe
qwd
qwc
qse
qsd
Qsc

and so on ...
The algorithm itself is important, because the values ​​of the array can be either letters or whole words

Виктор Щербаков   (2001-12-17 13:33) [1]

Three nested loops. One on the first column, another on the second, third on the third. What is the problem?

Дмитрий Д.   (2001-12-17 13:42) [2]

It's clear
the problem is I have a number of both rows and columns can be different ...
(I took 3x3 for example)
therefore, we need an algorithm

Виктор Щербаков   (2001-12-17 13:46) [3]

Then recursive iteration:
The function calls itself, passing in quality. parameter column number of the array. And so on until it reaches the last column (or the first). In the function itself, we get a single column loop.

Дмитрий Д.   (2001-12-17 13:59) [4]

and a piece of code is possible?
I thought about recursion, but I just can’t figure it out

Виктор Щербаков   (2001-12-17 14:11) [5]

Something like this (whipped up).
The array itself is filled with what you need.

`procedure TForm1.Button2Click (Sender: TObject); var a: array [1..3, 1..3] of char; CurLine: String; procedure Proc1 (Col: Integer); var i: Integer; begin for i: = 1 to 3 do begin if Col = 0 then begin Memo1.Lines.Add (CurLine); exit end; CurLine: = CurLine + a [i, col]; proc1 (Col - 1); SetLength (CurLine, Length (CurLine) - 1); end; end; begin CurLine: = ""; proc1 (3); end;`

Pages: 1 whole branch

Forum: "Main";
Current archive: 2002.01.08;