procedure Test(n: Integer); var i: integer; preValue: integer; strReslut: string; strIndex: string; begin if n < 1 then Exit; strIndex :='1'; strReslut :='1'; preValue := 1; for i := 2 to n do begin strIndex := strIndex + ';' + IntToStr(i); strReslut := strReslut+';'+IntToStr(preValue*i); preValue:=preValue*i; end; ShowMessage(strIndex+#13+strReslut); end;没仔细测试。
TIntArray = array of Integer; TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}//例程 procedure Calculate(var IntArray: TIntArray; N: Integer); var Index: Integer; begin SetLength(IntArray, Succ(N)); if N < 1 then Exit; IntArray[Low(IntArray)] := 1; for Index := Succ(Low(IntArray)) to High(IntArray) do begin IntArray[Index] := IntArray[Pred(Index)]*Index; end; end;//测试 procedure TForm1.Button1Click(Sender: TObject); var IntArray: TIntArray; begin Calculate(IntArray, 5); ShowMessage(IntToStr(IntArray[3])); end;
或者用控制台来显示更清晰program Project3;{$APPTYPE CONSOLE}uses SysUtils;type TIntArray = array of Integer;procedure Calculate(var IntArray: TIntArray; N: Integer); var Index: Integer; begin SetLength(IntArray, Succ(N)); if N < 1 then Exit; IntArray[Low(IntArray)] := 1; for Index := Succ(Low(IntArray)) to High(IntArray) do begin IntArray[Index] := IntArray[Pred(Index)]*Index; end; end;var IntArray: TIntArray; MyInt: Integer; Index: Integer; begin Index := 0; Calculate( IntArray, 10); for MyInt in IntArray do begin if Index > 0 then WriteLn(Format('%8d = %8d', [Index, MyInt])); Inc(Index); end; ReadLn; end.
type TIntArray = array of Integer;procedure Calculate(var IntArray: TIntArray; N: Integer); var Index: Integer; begin SetLength(IntArray, Succ(N)); if N < 1 then Exit; IntArray[Low(IntArray)] := 1; for Index := Succ(Low(IntArray)) to High(IntArray) do begin IntArray[Index] := IntArray[Pred(Index)]*Index; end; end;
procedure Test(n: Integer);
var
i: integer;
preValue: integer;
strReslut: string;
strIndex: string;
begin
if n < 1 then Exit;
strIndex :='1';
strReslut :='1';
preValue := 1;
for i := 2 to n do
begin
strIndex := strIndex + ';' + IntToStr(i);
strReslut := strReslut+';'+IntToStr(preValue*i);
preValue:=preValue*i;
end;
ShowMessage(strIndex+#13+strReslut);
end;没仔细测试。
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}//例程
procedure Calculate(var IntArray: TIntArray; N: Integer);
var
Index: Integer;
begin
SetLength(IntArray, Succ(N));
if N < 1 then Exit;
IntArray[Low(IntArray)] := 1;
for Index := Succ(Low(IntArray)) to High(IntArray) do
begin
IntArray[Index] := IntArray[Pred(Index)]*Index;
end;
end;//测试
procedure TForm1.Button1Click(Sender: TObject);
var
IntArray: TIntArray;
begin
Calculate(IntArray, 5);
ShowMessage(IntToStr(IntArray[3]));
end;
SysUtils;type
TIntArray = array of Integer;procedure Calculate(var IntArray: TIntArray; N: Integer);
var
Index: Integer;
begin
SetLength(IntArray, Succ(N));
if N < 1 then Exit;
IntArray[Low(IntArray)] := 1;
for Index := Succ(Low(IntArray)) to High(IntArray) do
begin
IntArray[Index] := IntArray[Pred(Index)]*Index;
end;
end;var
IntArray: TIntArray;
MyInt: Integer;
Index: Integer;
begin
Index := 0;
Calculate( IntArray, 10);
for MyInt in IntArray do
begin
if Index > 0 then
WriteLn(Format('%8d = %8d', [Index, MyInt]));
Inc(Index);
end;
ReadLn;
end.
type
TIntArray = array of Integer;procedure Calculate(var IntArray: TIntArray; N: Integer);
var
Index: Integer;
begin
SetLength(IntArray, Succ(N));
if N < 1 then Exit;
IntArray[Low(IntArray)] := 1;
for Index := Succ(Low(IntArray)) to High(IntArray) do
begin
IntArray[Index] := IntArray[Pred(Index)]*Index;
end;
end;
1 2 6 24 120 120x6 720x7 5040x8