京东在线笔试题,关于排列组合的问题

考虑数字序列{1,3,4,2,6,7,5,5,8,10,9,10,7,17},任取其中几个数字相加,使得到的和为29,
则不同的组合有几种?(第2,第3,第7,第14个数的组合与第2,第3,第13,第14个数的
组合看起来是一样的,都是3,4,5,17,那么这里只视为一种)

 using System;
using System.Linq;
using System.Collections.Generic;

public class Test
{
    static int[] d;
    public static void Main()
    {
        d = (new int[] {1,3,4,2,6,7,5,5,8,10,9,10,7,17}).OrderBy(x => x).ToArray();
        var query = foo(new int[]{}, 0).Select(item => string.Join(",", item)).Distinct();
        foreach (var item in query)
            Console.WriteLine(item);
    }
    static IEnumerable<IEnumerable<int>> foo(IEnumerable<int> seed, int n)
    {
        if (seed.Sum() == 29)
        {
            yield return seed;
        }
        else
        {
            if (seed.Sum() < 29)
            {
                for (int i = n; i < d.Count() - 1; i++)
                {
                    var items = foo(seed.Concat(d.Skip(i).Take(1)), i + 1);
                    foreach (var item in items) yield return item;
                }
            }
        }
    }

}
 1,2,3,4,5,5,9
1,2,3,4,5,6,8
1,2,3,4,5,7,7
1,2,3,4,9,10
1,2,3,5,5,6,7
1,2,3,5,8,10
1,2,3,6,7,10
1,2,3,6,8,9
1,2,3,7,7,9
1,2,4,5,7,10
1,2,4,5,8,9
1,2,4,6,7,9
1,2,4,7,7,8
1,2,5,5,6,10
1,2,5,5,7,9
1,2,5,6,7,8
1,2,6,10,10
1,2,7,9,10
1,3,4,5,6,10
1,3,4,5,7,9
1,3,4,6,7,8
1,3,5,5,6,9
1,3,5,5,7,8
1,3,5,6,7,7
1,3,5,10,10
1,3,6,9,10
1,3,7,8,10
1,4,5,5,6,8
1,4,5,5,7,7
1,4,5,9,10
1,4,6,8,10
1,4,7,7,10
1,4,7,8,9
1,5,5,8,10
1,5,6,7,10
1,5,6,8,9
1,5,7,7,9
1,6,7,7,8
1,8,10,10
2,3,4,5,5,10
2,3,4,5,6,9
2,3,4,5,7,8
2,3,4,6,7,7
2,3,4,10,10
2,3,5,5,6,8
2,3,5,5,7,7
2,3,5,9,10
2,3,6,8,10
2,3,7,7,10
2,3,7,8,9
2,4,5,5,6,7
2,4,5,8,10
2,4,6,7,10
2,4,6,8,9
2,4,7,7,9
2,5,5,7,10
2,5,5,8,9
2,5,6,7,9
2,5,7,7,8
2,7,10,10
2,8,9,10
3,4,5,7,10
3,4,5,8,9
3,4,6,7,9
3,4,7,7,8
3,5,5,6,10
3,5,5,7,9
3,5,6,7,8
3,6,10,10
3,7,9,10
4,5,5,6,9
4,5,5,7,8
4,5,6,7,7
4,5,10,10
4,6,9,10
4,7,8,10
5,5,9,10
5,6,8,10
5,7,7,10
5,7,8,9
6,7,7,9
9,10,10

http://ideone.com/BZ6FPB

82种

修正下,96种
http://ideone.com/kNl4Mw

 using System;
using System.Linq;
using System.Collections.Generic;

public class Test
{
    static int[] d;
    public static void Main()
    {
        d = (new int[] {1,3,4,2,6,7,5,5,8,10,9,10,7,17}).OrderBy(x => x).ToArray();
        var query = foo(new int[]{}, 0).Select(item => string.Join(",", item)).Distinct();
        foreach (var item in query)
            Console.WriteLine(item);
        Console.WriteLine(query.Count());
    }
    static IEnumerable<IEnumerable<int>> foo(IEnumerable<int> seed, int n)
    {
        if (seed.Sum() == 29)
        {
            yield return seed;
        }
        else
        {
            if (seed.Sum() < 29)
            {
                for (int i = n; i < d.Count(); i++)
                {
                    var items = foo(seed.Concat(d.Skip(i).Take(1)), i + 1);
                    foreach (var item in items) yield return item;
                }
            }
        }
    }

}

1,2,3,4,5,5,9
1,2,3,4,5,6,8
1,2,3,4,5,7,7
1,2,3,4,9,10
1,2,3,5,5,6,7
1,2,3,5,8,10
1,2,3,6,7,10
1,2,3,6,8,9
1,2,3,6,17
1,2,3,7,7,9
1,2,4,5,7,10
1,2,4,5,8,9
1,2,4,5,17
1,2,4,6,7,9
1,2,4,7,7,8
1,2,5,5,6,10
1,2,5,5,7,9
1,2,5,6,7,8
1,2,6,10,10
1,2,7,9,10
1,2,9,17
1,3,4,5,6,10
1,3,4,5,7,9
1,3,4,6,7,8
1,3,5,5,6,9
1,3,5,5,7,8
1,3,5,6,7,7
1,3,5,10,10
1,3,6,9,10
1,3,7,8,10
1,3,8,17
1,4,5,5,6,8
1,4,5,5,7,7
1,4,5,9,10
1,4,6,8,10
1,4,7,7,10
1,4,7,8,9
1,4,7,17
1,5,5,8,10
1,5,6,7,10
1,5,6,8,9
1,5,6,17
1,5,7,7,9
1,6,7,7,8
1,8,10,10
2,3,4,5,5,10
2,3,4,5,6,9
2,3,4,5,7,8
2,3,4,6,7,7
2,3,4,10,10
2,3,5,5,6,8
2,3,5,5,7,7
2,3,5,9,10
2,3,6,8,10
2,3,7,7,10
2,3,7,8,9
2,3,7,17
2,4,5,5,6,7
2,4,5,8,10
2,4,6,7,10
2,4,6,8,9
2,4,6,17
2,4,7,7,9
2,5,5,7,10
2,5,5,8,9
2,5,5,17
2,5,6,7,9
2,5,7,7,8
2,7,10,10
2,8,9,10
2,10,17
3,4,5,7,10
3,4,5,8,9
3,4,5,17
3,4,6,7,9
3,4,7,7,8
3,5,5,6,10
3,5,5,7,9
3,5,6,7,8
3,6,10,10
3,7,9,10
3,9,17
4,5,5,6,9
4,5,5,7,8
4,5,6,7,7
4,5,10,10
4,6,9,10
4,7,8,10
4,8,17
5,5,9,10
5,6,8,10
5,7,7,10
5,7,8,9
5,7,17
6,7,7,9
9,10,10
96