Function F(const N:UINT):UINT;//求第N项的值 var I : Integer; PreResult : Integer; ResultStored : Integer; begin Result := 0; PreResult := 1; for I := 1 to N do begin PreResult := ResultStored; ResultStored := Result; Result := ResultStored + PreResult; end; end;Function Sum(const n:UINT):UINT;//求和 begin result := F(N+2)-1; end;
Function F(const N:UINT):UINT;//求第N项的值 var I : Integer; PreResult : Integer; ResultStored : Integer; begin Result := 0; ResultStored := 1; for I := 1 to N do begin PreResult := ResultStored; ResultStored := Result; Result := ResultStored + PreResult; end; end;Function Sum(const n:UINT):UINT;//求和 begin result := F(N+2)-1; end;以前学汇编的时候,刚学到堆栈就做过一个这样的题,Push Result;Result := Result + StoredResult;Pop StoredResult;
来个正点的,用递归求解Function F(N:integer):integer; begin if (N = 2) or (N = 1) then Result := 1 else Result := F(N-1) + F(N-2); end;Function Sum(N:integer):integer; var i : integer; begin Result := 0; For i:= 1 to N do Result := Result + F(i); end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm3 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
Fibo: array of double;
public
{ Public declarations }
function GetInt(const AInts: array of double; const Index: Integer): double;
function Calc(const N: Cardinal): double; overload;
end;var
Form3: TForm3;implementation{$R *.dfm}uses
math;function TForm3.GetInt(const AInts: array of double;
const Index: Integer): double;
begin
Result := 0;
if InRange(Index, Low(AInts), High(AInts)) then
Result := AInts[Index];
end;function TForm3.Calc(const N: Cardinal): double;
var
I: Integer;
begin
SetLength(Fibo, N);
with ListBox1.Items do
begin
BeginUpdate;
try
Clear;
Fibo[0] := 1;
for I := 1 to High(Fibo) do
begin
Fibo[I] := GetInt(Fibo, i - 2) + GetInt(Fibo, i - 1);
Add(FormatFloat('#,##0', Fibo[I]));
end;
finally
EndUpdate;
end;
end; Result := Sum(Fibo);
end;procedure TForm3.FormCreate(Sender: TObject);
begin
Caption := FormatFloat('#,##0', Calc(100) );
end;end.
http://www.aoshu.cn/Article_D/2006-11/771381824292866.htm
var
I : Integer;
PreResult : Integer;
ResultStored : Integer;
begin
Result := 0;
PreResult := 1;
for I := 1 to N do
begin
PreResult := ResultStored;
ResultStored := Result;
Result := ResultStored + PreResult;
end;
end;Function Sum(const n:UINT):UINT;//求和
begin
result := F(N+2)-1;
end;
var
I : Integer;
PreResult : Integer;
ResultStored : Integer;
begin
Result := 0;
ResultStored := 1;
for I := 1 to N do
begin
PreResult := ResultStored;
ResultStored := Result;
Result := ResultStored + PreResult;
end;
end;Function Sum(const n:UINT):UINT;//求和
begin
result := F(N+2)-1;
end;以前学汇编的时候,刚学到堆栈就做过一个这样的题,Push Result;Result := Result + StoredResult;Pop StoredResult;
begin
if (N = 2) or (N = 1) then Result := 1
else Result := F(N-1) + F(N-2);
end;Function Sum(N:integer):integer;
var
i : integer;
begin
Result := 0;
For i:= 1 to N do Result := Result + F(i);
end;