Delphi-PRAXiS
Seite 1 von 3  1 23      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Zahlen-Kombinationen (https://www.delphipraxis.net/55220-zahlen-kombinationen.html)

bioser 18. Okt 2005 10:42


Zahlen-Kombinationen
 
Hallo, hat jemand eine Idee, wie man dies realisieren kann ?
Ich habe beispielsweise n Zahlen, mit denen ich n Kombinationen
erzeugen will.

Beispiel: 1 2 3 4

1 2 3 4
2 1 3 4
3 2 1 4
4 1 2 3

Im Prinzip wird immer das nächste Element nach vorne gepackt und dann die
restlichen Elemente angehängt. Dieser Algorithmus müßte auch irgendeinen
Namen haben. Komme aber nicht drauf.

Sharky 18. Okt 2005 10:50

Re: Zahlen-Kombinationen
 
Hai bioser,

was Du machen möchtest ist eine Permutation. Dazu gibt es von Hagen einen Beitrag in der Code-Library.

alzaimar 18. Okt 2005 11:00

Re: Zahlen-Kombinationen
 
Oder hier, ist kürzer:
Delphi-Quellcode:
Function NthPermutation (aString : String; aCount : Integer) : String;
Var
  d : Array Of Integer;
  g, i, n : Integer;

Begin
  n := Length (aString);
  setlength (d, n);
  d[1] := 1;
  For i := 2 to n - 1 do d[i] := i * d[i-1];
  Result := '';
  if aCount>=d[n-1]*n Then Exit;
  for i := n-1 downto 1 do begin
    g := (aCount div d[i]) + 1;
    Result := Result + aString[g];
    delete (aString, g, 1);
    aCount := aCount mod d[i];
    End;
  Result := Result + aString;
End;
So erzeugst Du alle Permutationen eines Strings ('12345'):
Delphi-Quellcode:
Var
  i : Integer;
  s : String;

Begin
  i:=0;
  Repeat
    s := NthPermuation ('12345',i);
    inc (i);
    if s<>'' Then Memo1.lines.add (s);
  Until s= '';
End;

bioser 18. Okt 2005 11:19

Re: Zahlen-Kombinationen
 
Zitat:

Zitat von alzaimar
So erzeugst Du alle Permutationen eines Strings ('12345'):
Delphi-Quellcode:
Var
  i : Integer;
  s : String;

Begin
  i:=0;
  Repeat
    s := NthPermuation ('12345',i);
    inc (i);
    if s<>'' Then Memo1.lines.add (s);
  Until s= '';
End;

Danke für Deine Antwort. Aber, ich weiß nicht, ob dies eine echte Permutation ist,
was ich haben möchte. Deine function spuckt mir folgende Permutationen aus:

54132
54213
54231
54312
54321

Ich brauche aber diese Kombinationen:

12345
21345
31245
41235
51234

Bei 1234 zum Beispiel so:

1234
2134
3124
4123

alzaimar 18. Okt 2005 11:31

Re: Zahlen-Kombinationen
 
Hä? Willst du alle Permutationen? Davon gibt es N! (bei einem String der Länge N), oder nur ganz bestimmte? Mein Algo sollte 120 Permutationen von '12345' aufzählen.

Beschreib doch nochmal genau, welche Du brauchst...

Aha... im Eingangsposting sah das Anders aus:
Das hier?
Delphi-Quellcode:
Function BioserPermutation (aString : String; aCount : Integer) : String;
Var
  s :String;

Begin
  Assert (aCount < Length (aString), 'Unzulässige Sequenznummer');
  s := Copy (aString, aCount+1, 1); // Dieses Zeichen soll nach vorne;
  Delete (aString, aCount+1,1); // Das Zeichen löschen
  Result := s + aString;
End;

bioser 18. Okt 2005 11:49

Re: Zahlen-Kombinationen
 
Ja, nochmal an einem Beispiel. Ich habe eine Liste von Zahlen:

1 2 3 4 //Das ist meine erste Kombination

Dann soll die zweite Zahl, also die "2", an die erste Stelle rücken
und die anderen in derselben Reihenfolge angehängt werden, nur daß die
"2" nicht berücksichtigt wird.

2 1 3 4 //Das ist meine zweite Kombination

Jetzt ist die nächste, die "3" an der Reihe, immer ausgehend von der
Originalreihenfolge in meiner Liste.

3 1 2 4 //Das ist meine dritte Kombination

und zuletzt diese Kombination

4 1 2 3

Hast Du jetzt verstanden, wie ich es haben möchte ?

alzaimar 18. Okt 2005 11:54

Re: Zahlen-Kombinationen
 
Das macht die Funktion "BioserPermutation"

freak4fun 18. Okt 2005 11:55

Re: Zahlen-Kombinationen
 
Hallo :hi:,

sowas?

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  lReihe: String;
  lTemp: Char;
begin
  lReihe := '1234';
  for i := 1 to length(lReihe) do
    begin
      lTemp := lReihe[1];
      lReihe[1] := lReihe[i];
      lReihe[i] := lTemp;
    end;
  ShowMessage(lReihe);
end;
Eins, zwei mehr Beispiele wären nicht schlecht. ;)

MfG
freak

alzaimar 18. Okt 2005 12:01

Re: Zahlen-Kombinationen
 
Schau doch mal:
Code:
BioserPermutation ('12345',0) = > '12345'
BioserPermutation ('12345',1) = > '21345'
BioserPermutation ('12345',2) = > '31245'
BioserPermutation ('12345',3) = > '41235'
BioserPermutation ('12345',4) = > '51234'
[edit]Korrigiert, sodass es auch funktioniert :oops: [/edit]

freak4fun 18. Okt 2005 12:08

Re: Zahlen-Kombinationen
 
Zitat:

Zitat von alzaimar
Code:
BioserPermutation ('12345',5) = > '51235'

Hier wird das Assert ausgelöst. :stupid: Vielleicht ist ihm das zu kompliziert. :zwinker:

MfG
freak


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:49 Uhr.
Seite 1 von 3  1 23      

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz