AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Sonstige Fragen zu Delphi Delphi aus mehreren Werten größte Kombination.
Thema durchsuchen
Ansicht
Themen-Optionen

aus mehreren Werten größte Kombination.

Ein Thema von Noobinator · begonnen am 9. Nov 2006 · letzter Beitrag vom 11. Nov 2006
Antwort Antwort
Seite 3 von 3     123   
Cöster

Registriert seit: 6. Jun 2006
589 Beiträge
 
Turbo Delphi für Win32
 
#21

Re: aus mehreren Werten größte Kombination.

  Alt 11. Nov 2006, 16:13
Hier mal eine zweite (rekursive) Lösung:

Delphi-Quellcode:
TIntArray = array of Integer;

TCombi = record
  Factors: array of Boolean;
  Value: Integer;
end;

{...}

function TForm1.GetCombi(const Ints: TIntArray; Combi: TCombi; I, Limit: Integer): TCombi;
var
  Combi2: TCombi;
begin
  Combi.Factors := Copy(Combi.Factors);
  if Combi.Factors[I] then
  begin
    Combi.Factors[I] := False;
    Dec(Combi.Value, Ints[I]);
  end;
  if I < High(Ints) then
  begin
    Combi2 := GetCombi(Ints, Combi, I + 1, Limit);
    Combi.Factors[I] := True;
    Inc(Combi.Value, Ints[I]);
    Result := GetCombi(Ints, Combi, I + 1, Limit);
    if Result.Value < Combi2.Value then
      Result := Combi2;
  end
  else
  begin
    Combi.Factors[I] := True;
    Inc(Combi.Value, Ints[I]);
    if Combi.Value > Limit then
    begin
      Combi.Factors[I] := False;
      Dec(Combi.Value, Ints[I]);
      if Combi.Value > Limit then
        Combi.Value := -1;
    end;
    Result := Combi;
  end;
end;
Zur Erklärung der Parameter:
Ints sind die Zahlen, aus denen die Kombination gefunden werden soll.
Combi speichert, welche Indizes aus Ints in der Kombination vorhanden sind und den Gesamtwert.
I ist der Index im Array von Combi, die geändert wird.
Limit ist die Obergrenze.

Wenn jemand Optimierungstipps hat, würde ich sie gerne hören.
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 3 von 3     123   


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:41 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz