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();
下面这个包括输出时间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();
static void FindCombination(int nSum, int nValue)
{
if (nValue < 0 || nSum < 0)
return; if (nSum > 0)
{
listComb.Push(nValue);
FindCombination(nSum - nValue, nValue - 1); listComb.Pop();
FindCombination(nSum, nValue - 1);
}
else
{
Console.WriteLine("组合:");
foreach (var item in listComb)
{
Console.WriteLine(item);
}
}
}
请问参数怎么传,
第一个是总和,比如你这里的20,第二个是一共有几个数,比如你这里的19
就是在nValue个从1到nValue连续数字里面,找出所有相加等于nSum的不相同数字
解释起来好拗口……-_-!
自己拿来试试就明白了~
List<List<int>> mylist = new List<List<int>>();
int length = myarray.Length;
for (int i = 0; i < Math.Pow(2, length); i++)
{
List<int> myint = new List<int>();
for (int j = 0; j < length; j++)
{
if (Convert.ToBoolean(i & (1 << j)))
myint.Add(myarray[j]);
}
mylist.Add(myint);
}
foreach (var a in mylist)
{
if (a.Sum() == 20)
{
foreach (var b in a)
{
Console.Write(b); Console.Write(",");
}
Console.WriteLine();
}
static void Main(string[] args)
{
double[] myarray = { 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 };
List<List<double>> mylist = new List<List<double>>();
int length = myarray.Length;
for (int i = 0; i < Math.Pow(2, length); i++)
{
List<double> myint = new List<double>();
for (int j = 0; j < length; j++)
{
if (Convert.ToBoolean(i & (1 << j)))
myint.Add(myarray[j]);
}
mylist.Add(myint);
}
foreach (var a in mylist)
{
if (a.Sum() == 1730.03)
{
foreach (var b in a)
{
Console.Write(b); Console.Write(",");
}
Console.WriteLine();
}
}
Console.WriteLine("ok");
Console.ReadKey(); }
内存溢出
这样的数组,我试过,把你程序改成数组不行的!
{myint.clear();
myint.Add(myarray[j]);}
搞忘清除了
请问if (Convert.ToBoolean(i & (1 << j)))中 1 << j 是什么意思???
using System.Collections.Generic;
using System.Linq;namespace ConsoleApplication1
{
class Program
{ static void Main(string[] args)
{
double[] myarray = { 1.1, 2.5, 3.3, 4.9, 5.8, 6.8, 7.5, 8.7, 9.4, 10.7, 11.2, 12.9, 13.6, 14.8, 15.9, 16.4, 17.7, 18.4, 19.2 };
int cnt = 0;
foreach (var result in 寻找组合(myarray, 50.8, myarray.Length - 1))
{
Console.Write("结果{0}==> ", ++cnt);
foreach (var n in result)
Console.Write("{0} ", n);
Console.WriteLine();
}
Console.WriteLine("--------------End");
Console.ReadKey();
} static IEnumerable<IEnumerable<double>> 寻找组合(double[] array, double sum, int index)
{
if (index >= 0)
{
foreach (var sub in 寻找组合(array, sum, index - 1))
yield return sub;
foreach (var sub in 寻找组合(array, sum - array[index], index - 1))
yield return sub.Concat(new double[] { array[index] });
if (Math.Abs(array[index] - sum) <= double.Epsilon)
yield return new double[] { array[index] };
}
} }
}你可以运行一下这个序,看看结果。然而这个随手写出的小程序不是我要说的重点,我要告诉你一个概念,这个概念才是重点。否则你在去应聘时很可能会“露怯”!有人说这个程序是递归程序,那就是完全不懂c#3.0语法了(现在c#语言版本早已经高于3.0版本了)。如果这是递归程序的话,里边不会写yield return的,而是会写一些return的。所以我要给你说的是一个更为基本的概念——迭代器。应该认识迭代器,不要误以为这是什么递归程序。
有多少使用c#的程序员需要“设计”(而不是一遍遍copy)程序的呢?如果是,那么会经常使用迭代器、泛型,lamda表达式等等。
double[] myarray = { 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 };
double [] myarray = { 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 };
List<List<double>> mylist = new List<List<double >>();
int length = myarray.Length;
//int ll = 0;
for (Int64 i = 0; i < Math.Pow(2, length); i++)
{
List<double> myint = new List<double>();
for (int j = 0; j < length; j++)
{
if (Convert.ToBoolean(i & (Convert.ToInt64(1) << j)))
myint.Add(myarray[j]);
}
mylist.Add(myint);
//Console.WriteLine(ll++);
foreach (var a in mylist)
{
if (a.Sum() == 1730.03)
{
foreach (var b in a)
{
Console.Write(b); Console.Write(",");
}
Console.WriteLine();
} }
mylist.Clear();
}
Console.ReadLine();
这样好像没得什么问题 了 有问题的话,你改 int i=0 改成 Int64 i=0,但是我不知道要执行多久才能执行完这个程序咯...
用25楼的方法你
假设你要用30个double去迭代,应该去用64位操作系统运行64位程序。
[/Quote]
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)
{
for (int i = index; i < array.Length; i++)
{
if ((value - array[i]) == 0)//找到一个组合
{
if ((sum- array[i]) == 0)
{
Console.WriteLine(str + "" + array[i]);
}
}
else if ((value - array[i]) > 0)//继续找
{
str += array[i] + ",";
sum = sum - array[i];
for (int j = i + 1; j < array.Length; j++)
{
Test(j, value - array[i], str, sum);
}
}
}
}
static void Main(string[] args)
{ for (int i = 0; i < array.Length; i++)
{
Test(i, 20,"",20);
}
}1,2,3,4,10
1,2,3,5,9
1,2,3,6,8
1,2,3,14
1,2,4,5,8
1,2,4,6,7
1,2,4,13
1,2,5,12
1,2,6,11
1,2,7,10
1,2,8,9
1,2,17
1,3,4,5,7
1,3,4,12
1,3,5,11
1,3,6,10
1,3,7,9
1,3,16
1,4,5,10
1,4,6,9
1,4,7,8
1,4,15
1,5,6,8
1,5,14
1,6,13
1,7,12
1,8,11
1,9,10
1,19
2,3,4,5,6
2,3,4,11
2,3,5,10
2,3,6,9
2,3,7,8
2,3,15
2,4,5,9
2,4,6,8
2,4,14
2,5,6,7
2,5,13
2,6,12
2,7,11
2,8,10
2,18
3,4,5,8
3,4,6,7
3,4,13
3,5,12
3,6,11
3,7,10
3,8,9
3,17
4,5,11
4,6,10
4,7,9
4,16
5,6,9
5,7,8
5,15
6,14
7,13
8,12
9,11
double[] myarray = { 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 };
看下花多长时间,没的10多分钟出不来,我试过了
SetListces.RemoveAt(i);
[\code]
SetListces.RemoveAt(i);
[\code]
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();
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();