c# linq 实现 m选n 组合

2020-04-24 14:39:39 蜻蜓队长

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

 

以上内容来自于网络,如有侵权联系即删除
相关文章

上一篇: spring+mybatis+shiro实战实现权限验证

下一篇: vue 开发小技巧

客服紫薇:15852074331
在线咨询
客户经理