Einzelnen Beitrag anzeigen

Benutzerbild von 3_of_8
3_of_8

Registriert seit: 22. Mär 2005
Ort: Dingolfing
4.129 Beiträge
 
Turbo Delphi für Win32
 
#15

Re: aus mehreren Werten größte Kombination.

  Alt 9. Nov 2006, 20:10
Ich bezog mich auf shmias Aussage und habe sie widerlegt.

Ich sage ja: Es ist NICHT das Rucksackproblem bzw. man könnte es als Sonderfall des Rucksackproblems sehen, aber schon ein sehr besonderer.

Prinzipiell würde ich das ganze so machen:

Delphi-Quellcode:
function FindCombination(a: TIntegerDynArray; maxValue: Integer): Integer;
var I, J, value, max: Integer;
begin
  I:=0;
  max:=0;
  while I<length(a) do
  begin
    if a[I]=maxValue then
    begin
      result:=a[I];
      exit;
    end else if a[I]<maxValue then
    begin
      value:=a[I];
      for J:=Succ(I) to high(a) do
        if value+a[J]<maxValue then inc(value, a[J]);
      if value>max then max:=value;
    end;
  inc(I);
  end;
  result:=max;
end;
EDIT: Dummer Fehler...
Manuel Eberl
„The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.“
- Terry Pratchett
  Mit Zitat antworten Zitat