<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 项的所有组合?

      时间:2023-10-25

      <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 项的所有组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:仅允许 .NET 中唯一项目的集合? 下一篇:以最优化的方式交叉两个集合

                相关文章

              • <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>
                  <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>