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
请按任意键继续. . .
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
请按任意键继续. . .
解决方案 »
- 在GridView或Repeter中,如果内容是true就显示1.gif,false就显示1.1.gif。怎么做?
- 如何实现像文本控件一样输入的下拉列表
- 高手++如何利用反射获取linq表中某一行的数据?
- 异常详细信息: System.Data.OleDb.OleDbException: 未指定的错误,
- 晕 好象js对treeview的checkbox的操作并不能改变真正checked属性!!!
- 最简单的问题!高分
- 格式控制
- 请问sqlserver中有“自动编号”的数据类型吗?
- 这个提示如何解决?
- 最傻的問題.送分.
- 新手:问个很弱智的问题。。。
- MVC 添加sql server数据库链接字符串
{
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));
}
恩 确实这样如果范围在0-3之间的个数超过log2(int.max)肯定死的很惨