Jump to content

permutações possíveis de um numero ou palavra


ecmlj

Recommended Posts

Bom dia no unigui tem alguma função para contar permutações possíveis de um numero ou palavra?
Tenho essa função mais não está funcionando corretamente,
Só funciona se os caracteres não se repetirem
Exemplo :
Qtda caracteres|combinação com 3 char| combinação com 4 char
123456=                              120                                       360
112345=                               72                                        192
112234=                               42                                         102
112233=                               24                                          54
111234=                               34                                          72
111223=                               19                                          38
111222=                                 8                                          14

function CountPermutations02(str: string; r: Integer): Int64;

  function Factorial(n: Integer): Int64;
  var
    I: Integer;
  begin
    Result := 1;
    for I := 2 to n do
      Result := Result * I;
  end;

  function Combination(n, r: Integer): Int64;
  begin
    Result := Factorial(n) div (Factorial(r) * Factorial(n - r));
  end;
var
  I: Integer;
  count: array of Integer;
  n: Int64;
begin
  SetLength(count, 256);
  for I := 1 to Length(str) do
    Inc(count[Ord(str[I])]);
  n := 1;
  for I := 0 to 255 do
    if count[I] <> 0 then
      n := n * Factorial(count[I]);
  Result := Combination(Length(str), r) * Factorial(r) div n;
end;

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...