算法!用C#写一个函数,在一个数组中找出随意几个值相加等于一个值

大小鱼 2012-09-12 02:31:26
比如,数组{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}
要找出那些数相加等于20,
请各位大侠不吝赐教,谢谢
...全文
3697 61 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
61 条回复
切换为时间正序
请发表友善的回复…
发表回复
木安 2014-04-17
  • 打赏
  • 举报
回复
不知道,java的行不行? http://blog.csdn.net/cxming007/article/details/23944863
ruleless 2012-09-20
  • 打赏
  • 举报
回复
因为20并不大,所以用搜索其实复杂度也不是很高(除非样例是那种一亿个数组元素全部都为1的极端情况)。
kevin_xieh 2012-09-19
  • 打赏
  • 举报
回复
[Quote=引用 41 楼 的回复:]

测试过了 结果为:
C# code
static int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };


private static void Test(int index, int value,string str,int sum)
……
[/Quote]



Console.WriteLine(str + "" + array[i]);
后面加个 break;
要不后面的还会执行,或是 把20放第一位,会出问题。
hanxiao08250825 2012-09-19
  • 打赏
  • 举报
回复
这个其实是典型的01背包问题,网上去查一下,效率非常高的。
沙徐 2012-09-18
  • 打赏
  • 举报
回复
背包问题
卿文天 2012-09-18
  • 打赏
  • 举报
回复
提个想法,先对数组进行遍历找出小于要求的数值进入临时数组,并对临时数组进行排序,排序后再来考虑求和算法。
whoozit 2012-09-18
  • 打赏
  • 举报
回复
应该先排序,然后寻找。
cy411129362 2012-09-18
  • 打赏
  • 举报
回复
[Quote=引用 55 楼 的回复:]

C# code
int[] i = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
var query = from t in i from t1 in i where t + t1 == 20 && t < t1 select new ……
[/Quote]搞错了,是多个数么。。。
cy411129362 2012-09-18
  • 打赏
  • 举报
回复
            int[] i = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
var query = from t in i from t1 in i where t + t1 == 20 && t < t1 select new { t, t1 };
O西瓜 2012-09-17
  • 打赏
  • 举报
回复
有点类似于背包问题,但又有区别,首先说明一下,背包不是一种算法,不存在背包算法,
背包是一个问题,解决该问题可以使用贪心,动态规划,分支界限技术,
贪心解决的问题是最优问题,但不一定最优,动态规划能找出最优解,但动态规划必须保证局部也是最优才有能用,这个问题使用分支界限技术可以在相对快的时间内找出来。
O西瓜 2012-09-17
  • 打赏
  • 举报
回复

DateTime dt1 = DateTime.Now;

double[] arr = new double[] { 1035.3, 257.67, 390, 296.64, 504, 613.56, 802.2, 1370.88, 316.1, 5.9, 291.33, 599.76, 68, 408, 756, 388.44, 578.34, 994.84, 649.74, 464.1, 77.47, 726.03, 340, 140.4, 952, 2223, 1404, 187.08, 1035.3, 140.42 };
Ocean o = new Ocean(arr, 504+408);
o.Build();

Console.WriteLine("用时:{0}毫秒" , (DateTime.Now - dt1).TotalMilliseconds);


结果:
找到解:504 340 68
找到解:504 408
用时:115.0066毫秒
请按任意键继续. . .
qdu123 2012-09-17
  • 打赏
  • 举报
回复
背包算法
大小鱼 2012-09-14
  • 打赏
  • 举报
回复
[C#=code]
SetListces.RemoveAt(i);
[\code]
大小鱼 2012-09-14
  • 打赏
  • 举报
回复
[C#=code]
SetListces.RemoveAt(i);
[\code]
lizhibin11 2012-09-14
  • 打赏
  • 举报
回复
下面这个包括输出时间130毫秒

int[] x = new int[20];
for (int i = 0; i < x.Length; i++)
x[i] = i + 1;
long start = 0;
long end = (long)Math.Pow(2, 20);
for (; start < end; start++)
{
int temp = 0;
for (int i = 0; i < 20; i++)
{
if(((start >> i) & 1) == 1)
{
temp += x[i];
}
}
if (temp == 20)
{
for (int i = 0; i < 20; i++)
{
if (((start >> i) & 1) == 1)
{
Console.Write("{0} ", x[i]);
}
}
Console.WriteLine();
}
}
Console.ReadLine();
lizhibin11 2012-09-14
  • 打赏
  • 举报
回复
包括输出时间,360多毫秒,我看看还能不能再快点。
lizhibin11 2012-09-14
  • 打赏
  • 举报
回复

int[] x = new int[20];
for (int i = 0; i < x.Length; i++)
x[i] = i + 1;
long start = 0;
long end = (long)Math.Pow(2, 20);
List<int> list = new List<int>(20);
for (; start < end; start++)
{
for (int i = 0; i < 20; i++)
{
if(((start >> i) & 1) == 1)
{
list.Add(x[i]);
}
}
if (list.Sum() == 20)
{
foreach (int p in list)
Console.Write("{0} ", p);
Console.WriteLine();
}
list.Clear();
}
Console.ReadLine();
andy1020tc 2012-09-13
  • 打赏
  • 举报
回复
你先要思考 你这个问题 解决的话 需要什么
首先 数组随意的值相加等于某一个值 那就需要两个这样的数组
然后要查找 就需要遍历数组 两组数组的话 就需要嵌套一次
然后 你再对照上面的代码 应该就能理解了
代码誊写工 2012-09-13
  • 打赏
  • 举报
回复
[Quote=引用 36 楼 的回复:]
其实25楼的方法不错!可是怎么改还是内存溢出。
[/Quote]
Quote=引用 26 楼 的回复:]
假设你要用30个double去迭代,应该去用64位操作系统运行64位程序。
[/Quote]
zhanglong19891129 2012-09-13
  • 打赏
  • 举报
回复
yield return a 是不是在当前迭代快中 add 上 a 啊????
加载更多回复(37)

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧