设有N 个自然数系列:1,2,... ,n
请编程求所有可能的包含所有系列数的组合.并保存到一个二维数组中.例如 N=5时,共有 N!=5!=5*4*3*2*1=120 种组合,数组则定义为 Arr(120,5):组合"12345"
Arr(1,1)=1 Arr(1,2)=2 Arr(1,3)=3 Arr(1,4)=4 Arr(1,5)=5组合"54321"
Arr(100,1)=5 Arr(100,2)=4 Arr(100,3)=3 Arr(100,4)=2 Arr(1,5)=1以及组合"13254",等等!
请编程求所有可能的包含所有系列数的组合.并保存到一个二维数组中.例如 N=5时,共有 N!=5!=5*4*3*2*1=120 种组合,数组则定义为 Arr(120,5):组合"12345"
Arr(1,1)=1 Arr(1,2)=2 Arr(1,3)=3 Arr(1,4)=4 Arr(1,5)=5组合"54321"
Arr(100,1)=5 Arr(100,2)=4 Arr(100,3)=3 Arr(100,4)=2 Arr(1,5)=1以及组合"13254",等等!
http://expert.csdn.net/Expert/topic/1445/1445587.xml?temp=.4868585
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
Arr: array of array of Integer;
function NArr(AInt: Integer): LongInt; //取阶乘的结果
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
f: Integer;
FLength: LongInt;
i,j: Integer;
begin
F := StrToInt(Edit1.Text);
FLength := NArr(F);
SetLength(Arr,FLength*f);
for i := 1 to FLength-1 do
for j := 1 to F-1 do
begin
NArr[i,j] := //下面这段自己加上好了
end;
end;function TForm1.NArr(AInt: Integer): LongInt;
begin
if AInt>1 then
result := AInt*NArr(AInt-1)
else
result := 1;
end;end.
设有N 个自然数系列:1,2,... ,n
请编程求所有可能的包含所有系列数的组合.并保存到一个二维数组中.例如 N=5时,共有 N!=5!=5*4*3*2*1=120 种组合,数组则定义为 Arr(120,5):比如:组合"12345","54321","13254",等等!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;const n=10;
type
Trecord=Record
N1:integer;
total:integer;
end;
TForm1 = class(TForm)
BitBtn1: TBitBtn;
function Value(i:integer):integer;
procedure BitBtn1Click(Sender: TObject);
private
list:array of Trecord;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function tform1.Value(i:integer):integer;
begin
list[i].N1:=i;
list[i].total:=list[i-1].total*list[i].N1;
inc(i);
if i>10 then
exit
else
value(i);
end;procedure TForm1.BitBtn1Click(Sender: TObject);
var i:integer;
begin
SetLength(list,n);
i:=1;
list[0].N1:=1;
list[0].total:=1;
value(i);
end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;const n=10;
type
Trecord=Record
N1:integer;
total:integer;
end;
TForm1 = class(TForm)
BitBtn1: TBitBtn;
function Value(i:integer):integer;
procedure BitBtn1Click(Sender: TObject);
private
list:array of Trecord;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function tform1.Value(i:integer):integer;
begin
list[i].N1:=i;
list[i].total:=list[i-1].total*list[i].N1;
inc(i);
if i>10 then
exit
else
value(i);
end;procedure TForm1.BitBtn1Click(Sender: TObject);
var i:integer;
begin
SetLength(list,n);
i:=1;
list[0].N1:=1;
list[0].total:=1;
value(i);
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;const n=10;
type
Trecord=Record
N1:integer;
total:integer;
end;
TForm1 = class(TForm)
BitBtn1: TBitBtn;
Edit1: TEdit;
function Value(i:integer):integer;
procedure FormShow(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
list:array of Trecord;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function tform1.Value(i:integer):integer;
begin
list[i].N1:=i;
list[i].total:=list[i-1].total*list[i].N1;
inc(i);
if i>10 then
exit
else
value(i);
end;procedure TForm1.FormShow(Sender: TObject);
var i:integer;
begin
SetLength(list,n);
i:=1;
list[0].N1:=1;
list[0].total:=1;
value(i);
end;procedure TForm1.BitBtn1Click(Sender: TObject);
var k:integer;
item of array of array integer;
begin
k:=StrToInt(edit1.text);
SetLength(item,list[k].total,list[k].N1);
end;end.
设有N 个自然数系列:1,2,... ,n
请编程求所有可能的包含所有系列数的组合.并保存到一个二维数组中.例如 N=5时,共有 N!=5!=5*4*3*2*1=120 种组合,数组则定义为 Arr(120,5):比如:组合"12345","54321","13254",等等!
var
i:integer ,R:longint;
begin
R:=1;
for i:=1 to n do
R:=R*i ;
Result:=R;
end;
var
N1, N2, N3, N4, N5: Integer;
begin
for N1 := 1 to 5 do
begin
for N2 := 1 to 5 do
begin
if N2 = N1 then Continue;
for N3 := 1 to 5 do
begin
if (N3 = N1) or (N3 = N2) then Continue;
for N4 := 1 to 5 do
begin
if (N4 = N1) or (N4 = N2) or (N4 = N3) then Continue;
N5 := 15 - N1 - N2 - N3 - N4;
AOutStrs.Append(Format('%d, %d, %d, %d, %d', [N1, N2, N3, N4, N5]));
end;
end;
end;
end;
end;
type
TForm1 = class(TForm)
Grid: TStringGrid;
Panel1: TPanel;
EdNum: TEdit;
BtnProcess: TButton;
procedure BtnProcessClick(Sender: TObject);
private
FNum, FNums: Integer;
public
procedure Init(N: Integer);
procedure Process(N: Integer);
function GetNs(N: Integer): Integer;
procedure InsertOfNum(N, APos, Rows: Integer);
procedure CopyRow(N, M: Integer);
end;var
Form1: TForm1;implementation{$R *.dfm}{ TForm1 }procedure TForm1.Init(N: Integer);
begin
FNum := N;
FNums := GetNs(N);
Grid.ColCount := FNum;
Grid.RowCount := FNums;
end;procedure TForm1.Process(N: Integer);
var
I, M: Integer;
begin
if N = 1 then
Grid.Cells[0, 0] := '1'
else
begin
Process(N-1);
M := GetNs(N-1);
CopyRow(N, M);
for I := N - 1 downto 0 do
InsertOfNum(N, I, M);
end;
end;procedure TForm1.BtnProcessClick(Sender: TObject);
begin
FNum := StrToInt(EdNum.Text);
if FNum > 8 then
FNum := 8;
if FNum < 1 then
FNum := 1;
Init(FNum);
Process(FNum);
end;function TForm1.GetNs(N: Integer): Integer;
var
I: Integer;
begin
Result := 1;
for I := 1 to N do
Result := Result * I;
end;procedure TForm1.InsertOfNum(N, APos, Rows: Integer);
var
I, J: Integer;
begin
for I := 0 to Rows - 1 do
begin
for J := N - 1 downto APos + 1 do
Grid.Cells[J, APos * Rows + I] := Grid.Cells[J - 1, APos * Rows + I];
Grid.Cells[APos, APos * Rows + I] := IntToStr(N);
end;
end;procedure TForm1.CopyRow(N, M: Integer);
var
I, J, L: Integer;
begin
for I := 1 to N - 1 do
begin
for L := 0 to M - 1 do
for J := 0 to N - 1 do
Grid.Cells[J, I * M + L] := Grid.Cells[J, I - 1 + L];
end;
end;end.