nanfei

c# linq 实现 m选n 组合

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

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)));
    }
}
// Define other methods and classes here

 

分类:

技术点:

相关文章:

  • 2021-09-26
  • 2021-11-30
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2022-01-03
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-07
相关资源
相似解决方案