假设有一些不固定长度的数组
比如
{"a","b","c",..........}
{"1","2","3",..........}这些数组的数目不固定, 比如有3组,4组甚至更多现在要得到所有数组中字符的组合(每个数组中的每一个元素和其他数组中的一个元素构成一个组合,有几个个数组,这个组合就有几个元素)
比如
a,1,....
a,2,.....
........
b,1,....
b,2,......
..............
请问要怎么计算?还有如何储存它们到数据库,
然后根据指定的元素得到这个组合
比如
{"a","b","c",..........}
{"1","2","3",..........}这些数组的数目不固定, 比如有3组,4组甚至更多现在要得到所有数组中字符的组合(每个数组中的每一个元素和其他数组中的一个元素构成一个组合,有几个个数组,这个组合就有几个元素)
比如
a,1,....
a,2,.....
........
b,1,....
b,2,......
..............
请问要怎么计算?还有如何储存它们到数据库,
然后根据指定的元素得到这个组合
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
static class Enumerable
{
public static IEnumerable<IEnumerable<TSource>> Combo<TSource>(this IEnumerable<IEnumerable<TSource>> source, IEnumerable<TSource> second)
{
foreach (var i in second)
{
foreach (var j in source)
{
List<TSource> list = new List<TSource>();
list.Add(i);
yield return j.Concat(list);
}
}
} public static IEnumerable<IEnumerable<TSource>> StartCombo<TSource>(this IEnumerable<TSource> first)
{
foreach (var i in first)
{
List<TSource> list = new List<TSource>();
list.Add(i);
yield return list;
}
}
} class Program
{
static void Main(string[] args)
{
List<List<string>> list = new List<List<string>>()
{
new List<string>() { "a" },
new List<string>() { "c", "d" },
new List<string>() { "e", "f", "g" },
new List<string>() { "h", "i" },
new List<string>() { "j" }
};
var result = list.Skip(1).Aggregate(list.First().StartCombo(), (serials, current) => serials.Combo(current), x => x).ToList();
result.ForEach(x => { x.ToList().ForEach(y => Console.Write(y + " ")); Console.WriteLine(); });
}
}
}
a c e h j
a d e h j
a c f h j
a d f h j
a c g h j
a d g h j
a c e i j
a d e i j
a c f i j
a d f i j
a c g i j
a d g i j
这种写法我就得比较优雅
我能不能在 "a c e h j" 这种输出结果中,插入我需要的字符呢?
比如变成 " a =衣服 c=裤子 e=袜子 h=鞋子 j=帽子"