【问题标题】:How to set the length of permutations of a given string?如何设置给定字符串的排列长度?
【发布时间】:2020-11-20 12:51:54
【问题描述】:

我有打印给定字符串所有可能排列的代码。如何设置一个排列的长度?例如,如果长度为 2,则输出应包含 ABCD 的 6 个可能排列。

class GFG 
{ 
    /** 
    * permutation function 
    * @param str string to 
    calculate permutation for 
    * @param l starting index 
    * @param r end index 
    */
    private static void permute(String str, 
                                int l, int r) 
    { 
        if (l == r) 
            Console.WriteLine(str); 
        else
        { 
            for (int i = l; i <= r; i++) 
            { 
                str = swap(str, l, i); 
                permute(str, l + 1, r); 
                str = swap(str, l, i); 
            } 
        } 
    } 

    /** 
    * Swap Characters at position 
    * @param a string value 
    * @param i position 1 
    * @param j position 2 
    * @return swapped string 
    */
    public static String swap(String a, 
                            int i, int j) 
    { 
        char temp; 
        char[] charArray = a.ToCharArray(); 
        temp = charArray[i] ; 
        charArray[i] = charArray[j]; 
        charArray[j] = temp; 
        string s = new string(charArray); 
        return s; 
    } 

// Driver Code 
public static void Main() 
{ 
    String str = "ABCD"; 
    int n = str.Length; 
    permute(str, 0, n-1); 
} 
} 

【问题讨论】:

  • 如果长度为2且有4个字符,则有12个潜在输出。
  • 如果输出的长度与输入的长度不同,则不是排列。来自wikipedia article:“在数学中,集合的排列是,松散地说,将其成员排列成序列或线性顺序,或者如果集合已经有序,则重新排列其元素。”您正在寻找一种从集合中选择特定大小的所有子集的方法。
  • @Martin 对不起。我对另一个例子感到困惑。

标签: c# permutation


【解决方案1】:

我最初错过了这是使用可变数量的数字排列(即可以生成的所有 2 个字符串中的 5 个源字符)。我已经对此进行了修改以显示更多细节。

这可能是一道数学题而不是编程题。

为了计算最大输出长度 l 的源字符 c 的排列数,请使用以下命令:

result = c! / (c - l)!

对于 4 个字符和最多 2 个字符输出的示例,这给出:

result = 4! / (4 - 2)!
       = 4! / 2!
       = 24 / 2
       = 12

在 C# 中计算这个并不难 - 计算 c 的阶乘 (!) 以及 cl 之间的差,然后将第一个除以第二个:

使用下面的便捷功能:

/// <summary>
/// Gets the factorial of the specified number.
/// </summary>
/// <param name="n">An int indicating the number to get the factorial of.</param>
/// <returns>An int indicating the factorial.</returns>
private static int getFactorial(int n)
{
    int returnValue = 1;

    while (n > 1)
        returnValue *= n--;

    return returnValue;
}

这可以使用:

int result = getFactorial(4) / getFactorial(4 - 2);
Console.WriteLine(result);

输出:

12

这也可以放在自己的函数中:

/// <summary>
/// Gets the factorial of n to length l.
/// </summary>
/// <param name="n">An int indicating the number of items in the set.</param>
/// <param name="l">An int indicating the length of the output.</param>
/// <returns>An int indicating the result.</returns>
private static int getFactorial(int n, int l)
{
    if (l > n) throw new ArgumentException("The legnth of the output may not be larger than the number of items", nameof(l));
    return getFactorial(n) / getFactorial(n - l);
}

这样使用:

Console.WriteLine($"The factorial of 4 by 2 is {getFactorial(4, 2)}");
Console.WriteLine($"The factorial of 5 by 3 is {getFactorial(5, 3)}");
Console.WriteLine($"The factorial of 6 by 2 is {getFactorial(6, 2)}");

输出:

The factorial of 4 by 2 is 12
The factorial of 5 by 3 is 60
The factorial of 6 by 2 is 30

注意:使用int 可能不是最佳选择。在低数字时它运行良好,但一旦你有一个 13 个字符的字符串 (13!),它就会溢出并给出无效的结果。 13! = 6,227,020,800 显然比 C# 中的 int32 大。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-03
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多