110,533
社区成员
发帖
与我相关
我的任务
分享
a1={0,1,2,3,4,5,6,7,8,9};
a2={0,1,2,3,4,5,6,7,8,9};
a3={0,1,2,3,4,5,6,7,8,9};
a4={0,1,2,3,4,5,6,7,8,9};
a5={0,1,2,3,4,5,6,7,8,9};
static void Main()
{
Stopwatch sw = new Stopwatch();
sw.Start();
StringBuilder sb = new StringBuilder();
var listofInts = new List<List<int>>(5);
listofInts.Add(new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
listofInts.Add(new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
listofInts.Add(new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
listofInts.Add(new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
listofInts.Add(new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
var temp = CartesianProduct(listofInts);
foreach (var item in temp)
{
sb.Append(string.Join("", item) + ",");
}
string result = sb.ToString();
result = result.Substring(0, result.Length - 1);
sw.Stop();
Console.WriteLine(result);
Console.WriteLine("共:" + result.Split(',').Length + "种组合");
Console.WriteLine("耗时:" + sw.ElapsedMilliseconds + "ms");
Console.ReadLine();
}
static void Main(string[] args)
{
int[] a1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] a2 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] a3 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] a4 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] a5 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int count = 0;
Stopwatch sw = new Stopwatch();
sw.Start();
var r = from i1 in a1
from i2 in a2
from i3 in a3
from i4 in a4
from i5 in a5
select i1 + "," + i2 + "," + i3 + "," + i4 + "," + i5;
count = r.Count();
sw.Stop();
TimeSpan ts2 = sw.Elapsed;
Console.WriteLine("共{0}种组合,耗时{1}ms", count, ts2.TotalMilliseconds);
sw.Restart();
count = 0;
foreach (var i1 in a1)
foreach (var i2 in a2)
foreach (var i3 in a3)
foreach (var i4 in a4)
foreach (var i5 in a5)
{
var n = i1 + "," + i2 + "," + i3 + "," + i4 + "," + i5;
count++;
}
sw.Stop();
ts2 = sw.Elapsed;
Console.WriteLine("共{0}种组合,耗时{1}ms", count, ts2.TotalMilliseconds);
Console.ReadKey();
}
private static IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from accseq in accumulator
from item in sequence
select accseq.Concat(new[] { item }));
}
static IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sequences, bool removeDuplicates, IEqualityComparer<IEnumerable<T>> sequenceComparer)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
var resultsSet = sequences.Aggregate(emptyProduct, (accumulator, sequence) => from accseq in accumulator
from item in sequence
select accseq.Concat(new[] { item }));
if (removeDuplicates)
return resultsSet.Distinct(sequenceComparer);
else
return resultsSet;
}
class UnorderedSequenceComparer : IEqualityComparer<IEnumerable<int>>
{
public bool Equals(IEnumerable<int> x, IEnumerable<int> y)
{
return x.OrderBy(i => i).SequenceEqual(y.OrderBy(i => i));
}
public int GetHashCode(IEnumerable<int> obj)
{
return obj.Sum(i => i * i);
}
}