Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中使用字符串数组生成组合#_C#_Arrays_Algorithm_Combinations - Fatal编程技术网

C# 在c中使用字符串数组生成组合#

C# 在c中使用字符串数组生成组合#,c#,arrays,algorithm,combinations,C#,Arrays,Algorithm,Combinations,可能重复: 如何使用组合公式nCr=10,每个组合生成2/3/4/5个字符串,例如每个组合生成2个字符串,没有重复/重复,也不考虑位置/2.(10-2)! = 45种组合 我需要输出如下: "01", "02" "01", "03" "01", "04" ... "02", "03" // eliminate the "02","01" 'cause it is same as "01","02" combination "02", "04" ... "01","02","03" "01",

可能重复:

如何使用组合公式nCr=10,每个组合生成2/3/4/5个字符串,例如每个组合生成2个字符串,没有重复/重复,也不考虑位置/2.(10-2)! = 45种组合

我需要输出如下:

"01", "02"
"01", "03"
"01", "04"
...
"02", "03" // eliminate the "02","01" 'cause it is same as "01","02" combination
"02", "04"
...
"01","02","03"
"01","02","04"
...
  var combinations = Combinations(0, 2);

  foreach ( var item in combinations )
    Console.WriteLine(item);
然后生成3个字符串的组合,将有120个组合(根据nCr)。 我需要输出如下:

"01", "02"
"01", "03"
"01", "04"
...
"02", "03" // eliminate the "02","01" 'cause it is same as "01","02" combination
"02", "04"
...
"01","02","03"
"01","02","04"
...
  var combinations = Combinations(0, 2);

  foreach ( var item in combinations )
    Console.WriteLine(item);
4个字符串的组合将有210个组合,至少每个组合有5个字符串的组合将有252个组合


我该怎么写呢?我使用了很多循环,看起来真的很乱。

您可以使用这个高效的项目:


下面是使用nCr对两个数字进行组合的函数,对多个数字进行调整

    /// <summary>
    /// Performs a nCr Combination of the two numbers
    /// </summary>
    /// <param name="n">The Number</param>
    /// <param name="r">The Range</param>
    /// <returns></returns>
    public static double Combination(double n, double r)
    {
        /*
         * Formula for Combination: n! / (r! * (n - r)!)
         */

        // n and r should be integral values
        double rfloor = Math.Floor(r);
        double nfloor = Math.Floor(n);
        // Check for all invalid values of n and r.
        if ((n < 1) || (r < 0) || (r > n) || (r != rfloor) || (n != nfloor))
        {
            throw new Exception("Invalid Input to Combination Function: Number must be greater than Range");
        }

        return Factorial(n) / (Factorial(r) * Factorial(n - r));
    }
//
///执行两个数字的nCr组合
/// 
///号码
///靶场
/// 
公共静态双组合(双n,双r)
{
/*
*组合公式:n!/(r!*(n-r)!)
*/
//n和r应该是整数值
双地板=数学地板(r);
双n层=数学层(n);
//检查n和r的所有无效值。
如果((n<1)| |(r<0)| |(r>n)| |(r!=r层)| |(n!=n层))
{
抛出新异常(“组合函数的输入无效:数字必须大于范围”);
}
返回阶乘(n)/(阶乘(r)*阶乘(n-r));
}

公共静态双阶乘(双n)
{
如果(n<0){抛出新异常(“不能取负数的阶乘”);}
双因子=1;
//0!和1!=1
对于(双i=2;i
您可以使用一个简单的递归:

private static IEnumerable<string> Combinations(int start, int level)
{
  for ( int i = start; i < array.Length; i++ )
    if ( level == 1 )
      yield return array[i];
    else
      foreach ( string combination in Combinations(i + 1, level - 1) )
        yield return String.Format("{0} {1}", array[i], combination);
}

我不是什么都懂,但它能帮你吗?用两个数字得到ncr组合的方法:或者:我需要组合,而不是总数combinations@TheMouthofaCow类似,但不相同。如何在代码中使用“Facet”?需要特定的命名空间/库吗?当然需要。我提供的还包含两个下载链接(在顶部)。一个用于演示程序,另一个用于源代码。这救了我,谢谢:)这是一个天才的解决方案。
private static IEnumerable<string> Combinations(int start, int level)
{
  for ( int i = start; i < array.Length; i++ )
    if ( level == 1 )
      yield return array[i];
    else
      foreach ( string combination in Combinations(i + 1, level - 1) )
        yield return String.Format("{0} {1}", array[i], combination);
}
  var combinations = Combinations(0, 2);

  foreach ( var item in combinations )
    Console.WriteLine(item);