设有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",等等!

解决方案 »

  1.   

    参照:
    http://expert.csdn.net/Expert/topic/1445/1445587.xml?temp=.4868585
      

  2.   

    如果高手给出DELPHI关于我问题的代码,立即送分!
      

  3.   

    interfaceuses
      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.
      

  4.   

    NArr[i,j] :=  //下面这段自己加上好了楼上的只是求排列!!我的贴子的意思是:
    设有N 个自然数系列:1,2,... ,n
    请编程求所有可能的包含所有系列数的组合.并保存到一个二维数组中.例如 N=5时,共有 N!=5!=5*4*3*2*1=120 种组合,数组则定义为 Arr(120,5):比如:组合"12345","54321","13254",等等!
      

  5.   

    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.
      

  6.   

    //list是個記錄數組,也類似于2維數組.你可以任意定義N的大小,其中list[0]的一組值不能算到里面,它是初始值.真正的值從list[1]開始.
    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.
      

  7.   

    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;
        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.
      

  8.   

    楼上的只是求表达式(N!)的数学计算,我是要求解 具体的组合!!我的贴子的意思是:
    设有N 个自然数系列:1,2,... ,n
    请编程求所有可能的包含所有系列数的组合.并保存到一个二维数组中.例如 N=5时,共有 N!=5!=5*4*3*2*1=120 种组合,数组则定义为 Arr(120,5):比如:组合"12345","54321","13254",等等!
      

  9.   

    求N! 的代码简单的要死:N!=1*2*3*...*n 就是:function GetN (N:integer)
      var
        i:integer ,R:longint;
      begin
        R:=1;
        for i:=1 to n do
            R:=R*i ;
        Result:=R;
      end;
      

  10.   

    问得不明白,如果就求N!那不简单的要死?再ARR(N!,N),哈,完了
      

  11.   

    这个问题很简单procedure SortQueue(AOutStrs: TStrings);
    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;
      

  12.   

    试试下面的代码:
    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.