using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
    class Program
    {
        class KVP
        {
            public int i { get; set; }
            public int x { get; set; }
        }        static void Main(string[] args)
        {
            int[] src = { 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1 };
            int min = 0;
            int max = 3;
            var data = src.Select((x, i) => new KVP { x = x, i = i });
            foreach (var item in foo(data, data.Select(x => new KVP[] { x }), min, max))
                Console.WriteLine(string.Join(",", item.Select(x => x.i)));
        }        static IEnumerable<IEnumerable<KVP>> foo(IEnumerable<KVP> data, IEnumerable<IEnumerable<KVP>> seed, int min, int max)
        {
            foreach (var item in seed)
            {
                if (item.Sum(x => x.x) >= min && item.Sum(x => x.x) <= max)
                    yield return item;
                if (item.Sum(x => x.x) <= max)
                { 
                    foreach (var i in foo(data, data.Where(y => y.i > item.Last().i).Select(y => item.Concat(new KVP[] { y })), min, max))
                        yield return i;
                }
            }
        }
    }
}0
0,27
1
26
27
请按任意键继续. . .

解决方案 »

  1.   

    static void Main(string[] args)
            {
                var res = GetResult(new int[] { 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75, 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1 }, 3, 0);            Console.WriteLine(string.Join("\n", res.ToList().Select(x => string.Join(",", x))));            Console.ReadLine();
            }        static IEnumerable<IEnumerable<int>> GetResult(int[] arrSource, int iMax, int iMin)
            {
                var vTemp = Enumerable.Range(0, arrSource.Length).Select(x => new { index = x, value = arrSource[x] }).Where(x => x.value >= iMin && x.value <= iMax).ToList();            return Enumerable.Range(0, 1 << vTemp.Count).Select(x => Enumerable.Range(0, vTemp.Count).Select(y => (x & (1 << y)) != 0 ? vTemp[y] : null).Where(z => z != null))
                    .Where(x => x.Count() > 0).Where(x => x.Sum(y => y.value) >= iMin && x.Sum(y => y.value) <= iMax).Select(x => x.Select(y => y.index));
            }
      

  2.   


    恩  确实这样如果范围在0-3之间的个数超过log2(int.max)肯定死的很惨