生成一字符串(例:‘0123456’)的所有字符相同的类似字符串(例:‘2345610’,‘6452301’,‘3564102’等等)?

解决方案 »

  1.   

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

  2.   

    zswangII(伴水清清)(职业清洁工):
    老兄,此函数得到的结果有重复的字符串,但数量是5040,说明肯定是有遗漏的字符串。
    望检查一下。
      

  3.   

    //这样检查是否有重复~~
      for I := Memo1.Lines.Count - 1 downto 0 do
        if Memo1.Lines.IndexOf(Memo1.Lines[I]) <> I then
          ShowMessage('Error');
    //这样计算组合数~~
    5040 = 7 * 6 * 5 * 4 * 3 * 2 * 1//老兄,先自己找找公式算一下,再来发贴~~