博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法一枚
阅读量:4501 次
发布时间:2019-06-08

本文共 1827 字,大约阅读时间需要 6 分钟。

///         ///  Find all sub number groups that sum equal to a given number        ///         /// sorted list        /// total number        /// 
all sub number groups
private static IEnumerable
> FindCombination(List
list, int total) { var found = new List
>(); //[2,3,4] total:1 if (list.First() > total) return found; //[2] total:1 if (list.Count == 1 && list.First() != total) return found; //[1,2, 6] => [1,2] if (list.Contains(total)) { found.Add(new List
{ total }); list.Remove(total); } //[1,2,3] => 1+2+3 =6 if (list.Sum() == total) { found.Add(list); return found; } //travel every item to find combination for (var i = 0; i < list.Count; i++) { var seed = list[i]; var subTotal = total - seed; var clonedList = new List
(list); clonedList.RemoveAt(i); var combinations = FindCombination(clonedList, subTotal); foreach (var item in combinations) { item.Add(seed); item.Sort(); if (!IsContainArray(found, item)) found.Add(item); } } return found; } ///
/// To check if an list of list contains a list /// ///
///
///
contains or not
private static bool IsContainArray(IEnumerable
> list, IEnumerable
item) { return list.Select(l => string.Join(",", l)).Contains(string.Join(",", item)); }
这个算法可以说是纯为实现而实现,没有经过任何精巧设计,但从测试结果来看,还没找到漏掉的。

转载于:https://www.cnblogs.com/deafcat/archive/2011/07/14/2106323.html

你可能感兴趣的文章
共享内存的设计
查看>>
deque容器
查看>>
2017-2018-1 20155203 20155204 实验二 固件程序设计
查看>>
三方贸易-drop ship
查看>>
Android RenderScript 使用 Struct 及其下标的赋值
查看>>
【题解】BZOJ P1801 dp
查看>>
杂项-软件生命周期:软件生命周期
查看>>
小程序:小程序能力
查看>>
P1578 奶牛浴场 有障碍点的最大子矩形
查看>>
OpenCV学习:体验ImageWatch
查看>>
socket_循环接收消息
查看>>
I/O基础之概念
查看>>
各种算法的优缺点:
查看>>
poj 2513 Colored Sticks 字典树
查看>>
BZOJ 1266: [AHOI2006]上学路线route Floyd_最小割
查看>>
Softmax函数
查看>>
.NET 向SQL里写入非Text类型
查看>>
HAOI2006 受欢迎的牛
查看>>
【代码备份】pocs.m
查看>>
(转)ApplicationDomain
查看>>