首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在c#中使用字符串数组生成组合

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

Stack Overflow用户
提问于 2012-12-04 08:50:12
回答 3查看 7.5K关注 0票数 2

可能重复: Different combinations of an array (C#)

代码语言:javascript
运行
复制
string[] array = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10"};

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

我需要这样的输出:

代码语言:javascript
运行
复制
"01", "02"
"01", "03"
"01", "04"
...
"02", "03" // eliminate the "02","01" 'cause it is same as "01","02" combination
"02", "04"
...

然后生成3个字符串的组合,将有120个组合(根据nCr)。我需要这样的输出:

代码语言:javascript
运行
复制
"01","02","03"
"01","02","04"
...

而4个字符串的组合,将有210个组合,最少,每组合5个字符串的组合,将有252个组合。

我怎么写呢?我消耗了很多循环,看起来真的很混乱。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-04 09:25:48

您可以使用一个简单的递归:

代码语言:javascript
运行
复制
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);
}

这样的称呼是:

代码语言:javascript
运行
复制
  var combinations = Combinations(0, 2);

  foreach ( var item in combinations )
    Console.WriteLine(item);
票数 9
EN

Stack Overflow用户

发布于 2012-12-04 09:04:56

您可以使用这个高效的项目:Permutations, Combinations, and Variations using C# Generics

代码语言:javascript
运行
复制
string[] array = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10" };
int lowerIndex = 2;
var combinations = new Facet.Combinatorics.Combinations<String>(
    array, 
    lowerIndex, 
    Facet.Combinatorics.GenerateOption.WithoutRepetition
);

foreach (IList<String> combis in combinations)
{
    String combi = String.Join(" ", combis);
    Console.WriteLine(combi);
}

因为它是开源的,所以您可以查看它是如何实现的。但是上面的链接也是非常有用的。

输出(lowerIndex=2):

代码语言:javascript
运行
复制
01 02
01 03
01 04
01 05
01 06
01 07
01 08
01 09
01 10
02 03  <-- no 02 01 since it would be a repitition
02 04
02 05
// ... (45 combinations w/o repetitions)
09 10

输出(lowerIndex=5):

代码语言:javascript
运行
复制
01 02 03 04 05
01 02 03 04 06
01 02 03 04 07
01 02 03 04 08
01 02 03 04 09
01 02 03 04 10
01 02 03 05 06
01 02 03 05 07
01 02 03 05 08
01 02 03 05 09
01 02 03 05 10
01 02 03 06 07
// ........... (252 combinations w/o repetitions)
05 07 08 09 10
06 07 08 09 10
票数 7
EN

Stack Overflow用户

发布于 2012-12-04 09:05:21

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

代码语言:javascript
运行
复制
    /// <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));
    }

代码语言:javascript
运行
复制
public static double Factorial(double n)
    {
        if (n < 0) { throw new Exception("Cannot take the factorial of a negative number"); }
        double factorial = 1;
        // 0! and 1! = 1
        for (double i = 2; i < n + 1; i++)
        {
            factorial *= i;
        }
        return factorial;
    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13699043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档