<tfoot id='mhQOH'></tfoot>
        <bdo id='mhQOH'></bdo><ul id='mhQOH'></ul>

      <legend id='mhQOH'><style id='mhQOH'><dir id='mhQOH'><q id='mhQOH'></q></dir></style></legend>
      <i id='mhQOH'><tr id='mhQOH'><dt id='mhQOH'><q id='mhQOH'><span id='mhQOH'><b id='mhQOH'><form id='mhQOH'><ins id='mhQOH'></ins><ul id='mhQOH'></ul><sub id='mhQOH'></sub></form><legend id='mhQOH'></legend><bdo id='mhQOH'><pre id='mhQOH'><center id='mhQOH'></center></pre></bdo></b><th id='mhQOH'></th></span></q></dt></tr></i><div id='mhQOH'><tfoot id='mhQOH'></tfoot><dl id='mhQOH'><fieldset id='mhQOH'></fieldset></dl></div>

      <small id='mhQOH'></small><noframes id='mhQOH'>

    1. 如何使用 LINQ 从一组数字中查找 n 项的所有组合?

      How to use LINQ to find all combinations of n items from a set of numbers?(如何使用 LINQ 从一组数字中查找 n 项的所有组合?)

      <tfoot id='FJOt5'></tfoot>
    2. <legend id='FJOt5'><style id='FJOt5'><dir id='FJOt5'><q id='FJOt5'></q></dir></style></legend>
      • <small id='FJOt5'></small><noframes id='FJOt5'>

              <tbody id='FJOt5'></tbody>

              • <bdo id='FJOt5'></bdo><ul id='FJOt5'></ul>
                <i id='FJOt5'><tr id='FJOt5'><dt id='FJOt5'><q id='FJOt5'><span id='FJOt5'><b id='FJOt5'><form id='FJOt5'><ins id='FJOt5'></ins><ul id='FJOt5'></ul><sub id='FJOt5'></sub></form><legend id='FJOt5'></legend><bdo id='FJOt5'><pre id='FJOt5'><center id='FJOt5'></center></pre></bdo></b><th id='FJOt5'></th></span></q></dt></tr></i><div id='FJOt5'><tfoot id='FJOt5'></tfoot><dl id='FJOt5'><fieldset id='FJOt5'></fieldset></dl></div>

                本文介绍了如何使用 LINQ 从一组数字中查找 n 项的所有组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试编写一个算法来从一组数字中选择 n 个值的所有组合.

                I'm trying to write an algorithm to select all combinations of n values from a set of numbers.

                例如,给定集合:1, 2, 3, 7, 8, 9

                集合中 2 个值的所有组合是:

                All combinations of 2 values from the set is:

                (1, 2), (1, 3), (1, 7), (1, 8), (1, 9), (2, 3), (2, 7), (2, 8), (2, 9), (3, 7), (3, 8), (3, 9), (7, 8), (7, 9), (8, 9)

                (1, 2), (1, 3), (1, 7), (1, 8), (1, 9), (2, 3), (2, 7), (2, 8), (2, 9), (3, 7), (3, 8), (3, 9), (7, 8), (7, 9), (8, 9)

                而 3 是:

                (1, 2, 3), (1, 2, 7), (1, 2, 8), (1, 2, 9), (1, 3, 7), (1, 3, 8), (1, 3, 9), (1, 7, 8), (1, 7, 9), (1, 8, 9), (2, 3, 7), (2, 3, 8), (2, 3, 9), (2, 7, 8), (2, 7, 9), (2, 8, 9), (3, 7, 8), (3, 7, 9), (3,8, 9), (7, 8, 9)

                (1, 2, 3), (1, 2, 7), (1, 2, 8), (1, 2, 9), (1, 3, 7), (1, 3, 8), (1, 3, 9), (1, 7, 8), (1, 7, 9), (1, 8, 9), (2, 3, 7), (2, 3, 8), (2, 3, 9), (2, 7, 8), (2, 7, 9), (2, 8, 9), (3, 7, 8), (3, 7, 9), (3, 8, 9), (7, 8, 9)

                等等!

                我目前正在使用方法来产生 2、3 和 4 值组合的返回集,但在我看来,这可以在 LINQ 查询中概括.

                I'm currently using methods to to yield return sets of combinations of 2, 3 and 4 values, but it seems to me this could be generalised in a LINQ query.

                感谢您的帮助!

                推荐答案

                用法:

                var results = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.DifferentCombinations(3);
                

                代码:

                public static class Ex
                {
                    public static IEnumerable<IEnumerable<T>> DifferentCombinations<T>(this IEnumerable<T> elements, int k)
                    {
                        return k == 0 ? new[] { new T[0] } :
                          elements.SelectMany((e, i) =>
                            elements.Skip(i + 1).DifferentCombinations(k - 1).Select(c => (new[] {e}).Concat(c)));
                    }
                }
                

                这篇关于如何使用 LINQ 从一组数字中查找 n 项的所有组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Adding and removing users from Active Directory groups in .NET(在 .NET 中的 Active Directory 组中添加和删除用户)
                set equality in linq(在 linq 中设置相等)
                HashSet conversion to List(HashSet 转换为 List)
                How to set timeout for webBrowser navigate event(如何为 webBrowser 导航事件设置超时)
                Test whether two IEnumerablelt;Tgt; have the same values with the same frequencies(测试两个IEnumerablelt;Tgt;具有相同频率的相同值)
                How do you determine if two HashSets are equal (by value, not by reference)?(您如何确定两个 HashSet 是否相等(按值,而不是按引用)?)
                • <legend id='rNPE0'><style id='rNPE0'><dir id='rNPE0'><q id='rNPE0'></q></dir></style></legend>

                  <small id='rNPE0'></small><noframes id='rNPE0'>

                      <bdo id='rNPE0'></bdo><ul id='rNPE0'></ul>
                        <tbody id='rNPE0'></tbody>
                      <i id='rNPE0'><tr id='rNPE0'><dt id='rNPE0'><q id='rNPE0'><span id='rNPE0'><b id='rNPE0'><form id='rNPE0'><ins id='rNPE0'></ins><ul id='rNPE0'></ul><sub id='rNPE0'></sub></form><legend id='rNPE0'></legend><bdo id='rNPE0'><pre id='rNPE0'><center id='rNPE0'></center></pre></bdo></b><th id='rNPE0'></th></span></q></dt></tr></i><div id='rNPE0'><tfoot id='rNPE0'></tfoot><dl id='rNPE0'><fieldset id='rNPE0'></fieldset></dl></div>

                        <tfoot id='rNPE0'></tfoot>