首页 > Windows开发 > 详细

c# linq 实现 m选n 组合

时间:2020-04-24 14:29:54      阅读:110      评论:0      收藏:0      [点我收藏+]
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

 

c# linq 实现 m选n 组合

原文:https://www.cnblogs.com/nanfei/p/12767222.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!