有三个字符,如:A,B,C.请列出它们所有的组合方式,如:ABC,ACB,CAB....
此问题还可以引申为:有N的字符,请列出它们所有的组合方式?

解决方案 »

  1.   

    这是一个阶层的问题
    ABC就有"3!=6"种答案!
    N!确实只要内嵌N-1个循环就可以了,但是着就是困难!
      

  2.   

    function pailie(s: string): tstrings;
    var 
     t:tstrings
    begin
      result:=tstring.create();
      if s = '' or length(s)  = 1;
      begin
      result.add(s);
      end;
      else
      begin
         t:= pailie(RightStr(s,Length(s) -1);
         for i:= 1 to t.count do 
           result.add(t[i] + s[1]);
         for i:= 1 to t.count do 
           result.add(s[1] + t[i]);
      end;
    end;调用
    var
    s;string;
    t:tstring;
    begin
    s:='ABCDEFG';
    t:= pailie(s);//t为结果
    end;
      

  3.   

    整整考虑了一天,终于可以了!
    //调试时需要一个Edit和两个ListBox;
    //ListBox1中就是结果!
    procedure TForm1.Button2Click(Sender: TObject);
    var
      MinValue,MaxValue,ForCounter,AgainCounter,QuitToken,ll:Integer;
      TempItem1,TempItem2,FormalData,ss,dd,WaitString:String;
    begin
      FormalData:=Trim(form1.Edit1.Text);
      ss:='';
      dd:='';
      for ForCounter:=0 to Length(FormalData)-1 do
      begin
        ss:=ss+'1';
        dd:=dd+IntToStr(Length(FormalData));
      end;
      MinValue:=StrToInt(ss);
      MaxValue:=StrToInt(dd);
      //例如长度为四时,最大为4444,最小为1111,然后寻找包含1234的数字
      for ForCounter:=MinValue to MaxValue do
      begin
        QuitToken:=0;
        WaitString:=Trim(IntToStr(ForCounter));
        for AgainCounter:=1  to Length(FormalData) do
          if Pos(Trim(IntToStr(AgainCounter)),WaitString)=0 then
          begin
            QuitToken:=1;
            break;
          end
          else continue;
        if QuitToken=0 then form1.ListBox2.Items.Add(WaitString);
      end;
      for  ForCounter:=0 to form1.ListBox2.Count-1 do
      begin
        TempItem1:='';
        TempItem2:=form1.ListBox2.Items.Strings[ForCounter];
        for AgainCounter:=0 to Length(FormalData)-1 do
        begin
          ll:=StrToInt(Trim(Copy(TempItem2,AgainCounter+1,1)));
          TempItem1:=TempItem1+Copy(FormalData,ll,1);
        end;
        form1.ListBox1.Items.Add(TempItem1);
      end;
    end;