如:有3个数 它的排列有6钟 
1 2 3 
1 3 2
2 1 3 
2 3 1
3 1 2
3 2 1
现在怎么得到这6种的数组

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type TMultiArr = array of array of integer;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Memo1: TMemo;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        function GetArr(arr: array of integer;var rs:TMultiArr):boolean;
        function GetFactorial(num:integer):Longword;
      end;var
      Form1: TForm1;implementation{$R *.dfm}{ TForm1 }function TForm1.GetArr(arr: array of integer;
      var rs:TMultiArr): boolean;
    var
      i,j,k:integer;
      tmpArr:array of integer;
    begin
      Result := True;
      SetLength(tmpArr,High(arr)-Low(arr));
      for i:= Low(arr) to High(arr) do
      begin
        k:=0;
        for j := Low(arr) to High(arr) do
        begin
          if i<>j then
          begin
            tmpArr[k] := arr[j];
            Inc(k);
          end;
        end;
        if (High(tmpArr)-Low(tmpArr)=0) then
        begin
          SetLength(rs,High(rs)-Low(rs)+2);
        end;
        if (High(tmpArr)-Low(tmpArr)>-1) then
        begin
          Memo1.Lines.Add(IntToStr(tmpArr[Low(tmpArr)]) + ':' + IntToStr(tmpArr[High(tmpArr)]));
          GetArr(tmpArr,rs);
          k := High(rs)-Low(rs);
          for j := GetFactorial(High(tmpArr)-Low(tmpArr)+1) downto 1 do
          begin
            SetLength(rs[k],High(rs[k])-Low(rs[k])+2);
            rs[k,High(rs[k])-Low(rs[k])] := arr[i];
            dec(k);
          end;
        end;
      end;
      if High(arr)-Low(arr)=0 then
      begin
        SetLength(rs[High(rs)-Low(rs)],High(rs[High(rs)-Low(rs)])-Low(rs[High(rs)-Low(rs)])+2);
        rs[High(rs)-Low(rs),High(rs[High(rs)-Low(rs)])-Low(rs[High(rs)-Low(rs)])] := arr[High(arr)];
      end;
      Result := True;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      a,b: TMultiArr;
      c:array of integer;
    begin
      SetLength(a,2);
      SetLength(a[0],2);
      SetLength(a[1],2);
      a[0,0]:=1;
      a[0,1]:=2;
      a[1,0]:=3;
      a[1,1]:=4;
      b := a;
      showmessage(IntToStr(b[0,1]));
      showmessage(IntToStr(High(c)-Low(c)));
    end;procedure TForm1.Button2Click(Sender: TObject);
    var
      rs:TMultiArr;
    begin
      GetArr([1,2,3,4,5],rs);
      showmessage(IntToStr(High(rs)-Low(rs)+1));
    end;function TForm1.GetFactorial(num:integer): Longword;
    begin
      if num < 0 then
      begin
        Result := 0;
        Exit;
      end;
      if num<=1 then
      begin
        Result := 1;
        Exit;
      end;
      Result := num * GetFactorial(num-1);
    end;procedure TForm1.Button3Click(Sender: TObject);
    begin
      showmessage(IntToStr(GetFactorial(5)));
    end;end.
      

  2.   

    to andrew223(暴风雪) 
    好象不对哦
      

  3.   

    program Arrange_Combine;{$APPTYPE CONSOLE}uses
      SysUtils;type TChSet=set of 'A'..'Z';function Arranege(Cn, Cm: byte): integer;
      procedure GenData(m, n: byte; t:string; s:TchSet);
      var c:char;
      begin
        if Length(t)=n then begin write(t:n+2); Result:=Result+1; end
           else for c:='A' to Chr(64+m) do
                    if not (c in s) then GenData(m, n, t+c, s+[c]);
      end;
    begin
      Result:=0;
      GenData(Cn, Cm, '', []);
    end;function Combine(Pn, Pm: byte): integer;
      procedure GenData(m, n: byte; t:string; s:TchSet);
      var c, c1:char;
      begin
        if Length(t)=n then begin write(t:n+2); Result:=Result+1; end else
           begin
             if Length(t)=0 then c1:='A' else c1:=Succ(t[Length(t)]);
             for c:=c1 to Chr(64+m) do
                 if not (c in s) then GenData(m, n, t+c, s+[c]);
           end;
      end;
    begin
      Result:=0;
      GenData(Pn, Pm, '', []);
    end;begin
      { TODO -oUser -cConsole Main : Insert code here }
      writeln('Arrange:');
      writeln(' Total=', Arranege(5, 3));
      writeln('Combine:');
      writeln(' Total=', Combine(5, 3));
      readln;
    end.绝对可用的
    http://lysoft.7u7.net
      

  4.   

    不对,那里不对告诉我一下。我在Debug
      

  5.   

    得到的数据不是我要的 
     ly_liuyang(Liu Yang) 他做的可以 了
      

  6.   

    //try
    function Collocate(mStrings: TStrings; mStr: string): Boolean; { 全排列 }
      procedure pCollocate(mLeft, mRight: string);
      var
        I, L: Integer;
        Temp: string;
      begin
        L := Length(mLeft);
        if L = 0 then
          mStrings.Add(mRight)
        else for I := 1 to L do begin
          Temp := mLeft;
          Delete(Temp, I, 1);
          pCollocate(Temp, Concat(mRight, mLeft[I]));
        end;
      end;
    begin
      Result := False;
      if not Assigned(mStrings) then Exit;
      mStrings.BeginUpdate;
      try
        mStrings.Clear;
        pCollocate(mStr, '');
      finally
        mStrings.EndUpdate;
      end;
      Result := True;
    end; { Collocate }procedure TForm1.Button1Click(Sender: TObject);
    begin
      Collocate(Memo1.Lines, '12345')
    end;
      

  7.   

    zswang(伴水清清)(专家门诊清洁工) :
      
       你的程序有个问题,就是如果要排列的数都不重复,比如1234,那么得出来的结果24组没错   但是如果是1123,那么你的程序的出来的结果就会出现重复。
       如果是1111,应该来说只有1组,但是你的程序得出24组1111。