Delphi的递推

----------------------

规则路线:上一个数只能走正下方和右下方,比如下图,3只能走45和8,45只能走0和97,以此规则找出某路线数字的最大和(路线上所有的数字之和,要求是最大和,任意一条路线的数字和都不能比您找出的这个最大和大),这里只求最大和,不管具体路线!

如果按正常的递归,由上而下的去获取路线最大值相加和,需要的时间复杂度将是2的n次方,

如果随机的是64行,将是2的64次方,这应该就要死机了,本人没有试过!

运用递推,设置20000行也很快能得出答案

---------------------------------

---------Unit开始------

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Edit2: TEdit;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
array01:array of array of Integer;
const
Memo_Row=51; //50行
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
a,b,i,j,R:Integer;
str01:string;
begin
i:=StrToIntDef (Edit1.Text,10);
j:=i;
SetLength(array01,i);
Randomize ;
Memo1.Clear ;
for a:=0 to (j-1) do
begin
SetLength(array01[a],a+1);
end;
for a:=Low(array01) to High(array01) do
begin
for b:=Low(array01[a]) to High(array01[a]) do
begin
R:=Random (100);
array01[a][b]:=R;
if i<Memo_Row then
begin
if b<>0 then
begin
if Length(IntToStr(R))>1 then
begin
str01:=str01+' '+ IntToStr(R);
end
else
begin
str01:=str01+' '+ IntToStr(R);
end;

end
else
begin
if Length(IntToStr(R))>1 then
begin
str01:= IntToStr(R);
end
else
begin
str01:= ' '+IntToStr(R);
end;

end;
end;

end;
if i<Memo_Row then
Memo1.Lines.Add(str01);
end;
//ShowMessage('0');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
Maxsum_array:array of Integer ;
int01,i,j:Integer;
str01:string;
begin
int01:=0;
int01:=Length (array01);
i:=0;
j:=0;


if int01<>0 then //數組不為空
begin
SetLength(Maxsum_array,int01);
i:=high(array01);
for j:=high(array01[i]) downto low(array01[i]) do
begin
Maxsum_array[j]:=array01[i][j] ;
end;

for i:=high(array01) downto low(array01) do
begin
if i<>(int01-1) then
begin
for j:=low(array01[i]) to high(array01[i]) do
begin
//Edit2.Text :=Edit2.Text+' '+inttostr(i);

if (Maxsum_array[j])>(Maxsum_array[j+1]) then
begin
//str01:=str01+IntToStr(Maxsum_array[j])+' '+inttostr(array01[i][j])+';' ;
Maxsum_array[j]:=Maxsum_array[j]+ (array01[i][j]);
end
else
begin
//str01:=str01+IntToStr(Maxsum_array[j+1])+' '+inttostr(array01[i][j])+';' ;
Maxsum_array[j]:=Maxsum_array[j+1]+ (array01[i][j]);
end;
end;
end;

end;
//Memo2.Text :=str01;
//ShowMessage(IntToStr(Maxsum_array[0]));
Edit2.Text:= IntToStr(Maxsum_array[0]);
end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Application.HintColor:=clRed;
Application.HintHidePause:=60*1000;
end;

end.

---------Unit结束-------

--------Form开始------------------

object Form1: TForm1
Left = 672
Top = 373
Caption = 'Form1'
ClientHeight = 510
ClientWidth = 644
Color = clBtnFace
Constraints.MaxHeight = 548
Constraints.MaxWidth = 660
Constraints.MinHeight = 548
Constraints.MinWidth = 660
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Memo1: TMemo
Left = 0
Top = 0
Width = 449
Height = 510
Align = alLeft
ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861
Lines.Strings = (
'Memo1')
ScrollBars = ssBoth
TabOrder = 0
end
object Button1: TButton
Left = 455
Top = 35
Width = 153
Height = 25
Caption = #36171#20540#38543#26426#19977#35282
TabOrder = 1
OnClick = Button1Click
end
object Edit1: TEdit
Left = 472
Top = 8
Width = 81
Height = 21
ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861
TabOrder = 2
Text = '3'
end
object Button2: TButton
Left = 455
Top = 136
Width = 164
Height = 25
Hint = #19978#38754#30340#25968#21482#33021#36208#19979#38754#21644#21491#36793#65292#25214#20986#26368#22823#36335#32447#65288#35813#36335#32447#20013#25968#23383#20043#21644#26368#22823#65289#65292#30446#21069#21482#27714#26368#22823#30340#21644#65292#19981#35201#27714#36335#32447
Caption = #33719#21462#36335#32447#26368#22823#30456#21152#20540
ParentShowHint = False
ShowHint = True
TabOrder = 3
OnClick = Button2Click
end
object Edit2: TEdit
Left = 472
Top = 109
Width = 121
Height = 21
ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861
TabOrder = 4
end
object Memo2: TMemo
Left = 455
Top = 167
Width = 185
Height = 186
ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861
Lines.Strings = (
'Memo2')
ScrollBars = ssBoth
TabOrder = 5
end
end

-------Form结束-----------------

原文地址:https://www.cnblogs.com/dmqhjp/p/14155588.html