从套的可能性找到所有组合 [英] Finding all combinations from sets of possibilities

查看:110
本文介绍了从套的可能性找到所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有多套包含连接了,我使用搞清楚数学值的附加阵列阵列。为了找到这些东西的最佳组合,我需要混合,并从这些阵列相匹配。我见过的解决方案类似这样的身边,但他们通常是没有真正的组合/ 1的可能性深阵列。因此,要举一个例子。

I have multiple sets of arrays that contain additional arrays that have values attached that I use for figuring out math. In order to find the best combination of these things, I need to mix and match from these arrays. I've seen "solutions" similar to this around, but they're usually 1 array deep with no real combinations/possibilities. So to give an example.

我已经设置A,B,和C组A包含AA,AB,AC,和广告。 AA包含一套价值。推断是出于对他人。氨基酸只能与Ba和Ca的比我该如何去写一个程序,找出所有组合(即AA,AB,抄送,屋宇署与BA,CB,AC,BD与等相比),所以我可以比较每个组合数学找到最好的呢?注意:这仅仅是一个例子,我并不需要它用于特异性3套4套4,它需要能够扩展

I have sets A, B, and C. Set A contains Aa, Ab, Ac, and Ad. Aa contains a set of values. Extrapolate that out for the others. Aa can only be compared with Ba and Ca. How do I go about writing a program to find all combinations(i.e. Aa, Ab, Cc, Bd compared with Ba, Cb, Ac, Bd and etc) so I can compare the math on each combination to find the best one? Note: this is just an example, I don't need it for specifically 3 sets of 4 sets of 4, it needs to be able to expand.

现在我知道我没有为我的变量使用非常有意义的名字,但我会AP preciate如果有任何code确实有有意义的名字在它(我真的宁可不跟随周边变​​量在code X和C左右)。

Now I know I didn't use very meaningful names for my variables, but I would appreciate if any code given does have meaningful names in it(I'd really rather not follow around variables of x and c around in code).

推荐答案

接受的答案似乎是正确的,但一个非常做的怪笛卡尔积在C#的方式。如果你有序列的给定数量,你可以把它们的笛卡尔乘积惯用这样的:

The accepted answer appears to be correct but is a very strange way to do a Cartesian product in C#. If you have a given number of sequences you can take their Cartesian product idiomatically like this:

    var aList = new[] { "a1", "a2", "a3" };
    var bList = new[] { "b1", "b2", "b3" };
    var cList = new[] { "c1", "c2", "c3" };
    var product = from a in aList
                  from b in bList
                  from c in cList
                  select new[] { a, b, c };

    foreach (var p in product)
        Console.WriteLine(string.Join(",", p));

如果您有需要采取它们的笛卡尔乘积则任意多个序列,你可以做这样的:

If you have arbitrarily many sequences that you need to take their Cartesian product then you can do it like this:

static class Extensions
{
  public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(
    this IEnumerable<IEnumerable<T>> sequences) 
  { 
    IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() }; 
    return sequences.Aggregate( 
      emptyProduct, 
      (accumulator, sequence) => 
        from accseq in accumulator 
        from item in sequence 
        select accseq.Concat(new[] {item})); 
  }
}

和则:

    var aList = new[] { "a1", "a2", "a3" };
    var bList = new[] { "b1", "b2", "b3" };
    var cList = new[] { "c1", "c2", "c3" };
    var lists = new[] { aList, bList, cList };
    var product = lists.CartesianProduct();
    foreach (var p in product)
        Console.WriteLine(string.Join(",", p));

请参阅

http://ericlippert.com/2010 / 06/28 /计算-A-笛卡尔产品与-LINQ /

和我的答案

生成所有可能的组合

有关这个问题的更多讨论。

for more discussion of this problem.

这篇关于从套的可能性找到所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆