笛卡尔乘积也就是多个集合交叉排列,详情见百度百科
1 /// <summary> 2 /// 笛卡尔乘积 a{1,2} b{a,b} 显示为 1a,1b,2a,2b 3 /// </summary> 4 private static void DiKaer() 5 { 6 List<List<string>> list = new List<List<string>>(); 7 List<string> list1 = new List<string>(); 8 list1.Add("1"); 9 list1.Add("2"); 10 list1.Add("3"); 11 list1.Add("4"); 12 List<string> list2 = new List<string>(); 13 list2.Add("a"); 14 list2.Add("b"); 15 list2.Add("c"); 16 list2.Add("d"); 17 list2.Add("e"); 18 List<string> list3 = new List<string>(); 19 list3.Add("i"); 20 list3.Add("j"); 21 list3.Add("k"); 22 list.Add(list1); 23 list.Add(list2); 24 list.Add(list3); 25 List<string> result = new List<string>(); 26 string str = "Test"; 27 run(list, result, 0, str); 28 Console.WriteLine("笛卡尔乘积:"); 29 foreach (var item in result) 30 { 31 Console.Write(item + ","); 32 } 33 Console.WriteLine(); 34 Console.WriteLine("遍历循环:"); 35 result = new List<string>(); 36 run3(list, result, str); 37 foreach (var item in result) 38 { 39 Console.Write(item + ","); 40 } 41 Console.WriteLine(); 42 result = new List<string>(); 43 run2(list, result, 0, str); 44 Console.WriteLine("笛卡尔乘积引申:"); 45 foreach (var item in result) 46 { 47 Console.Write(item + ","); 48 } 49 } 50 private static void run(List<List<string>> dimvalue, List<string> result, int layer, string curstring) 51 { 52 if (layer < dimvalue.Count - 1)//如果下标小于集合的个数-1 53 { 54 if (dimvalue[layer].Count == 0)//集合下标中的集合个数为0 55 { 56 run(dimvalue, result, layer + 1, curstring); 57 } 58 else 59 { 60 for (int i = 0; i < dimvalue[layer].Count; i++)//循环集合下标中的元素 61 { 62 StringBuilder sb = new StringBuilder(); 63 sb.Append(curstring); 64 sb.Append(dimvalue[layer][i]);//添加到返回结果集汇总 65 run(dimvalue, result, layer + 1, sb.ToString());//增加下标索引继续循环元素 66 } 67 } 68 } 69 else if (layer == dimvalue.Count - 1)//如果下标等于集合的个数-1 70 { 71 if (dimvalue[layer].Count == 0)//如果下标的个数为0 72 result.Add(curstring); 73 else 74 { 75 for (int i = 0; i < dimvalue[layer].Count; i++) 76 { 77 result.Add(curstring + dimvalue[layer][i]); 78 } 79 } 80 } 81 }
笛卡尔乘积 另一种实现方式,循环遍历所有集合(不通用)
1 /// <summary> 2 /// 在知道有具体个数数组的情况下进行循环遍历 3 /// </summary> 4 /// <param name="dimvalue"></param> 5 /// <param name="result"></param> 6 /// <param name="curstring"></param> 7 private static void run3(List<List<string>> dimvalue, List<string> result, string curstring) 8 { 9 foreach (var item in dimvalue[0]) 10 { 11 foreach (var item2 in dimvalue[1]) 12 { 13 foreach (var item3 in dimvalue[2]) 14 { 15 result.Add(string.Format("{0}{1}{2}{3}", curstring, item, item2, item3)); 16 } 17 } 18 } 19 }
由笛卡尔乘积引申出来进行一对一的排列
1 /// <summary> 2 /// 笛卡尔乘积引申 3 /// </summary> 4 /// <param name="dimvalue">初始集合</param> 5 /// <param name="result">返回集合</param> 6 /// <param name="layer">从第几个下标开始</param> 7 /// <param name="curstring">集合前的字符串</param> 8 private static void run2(List<List<string>> dimvalue, List<string> result, int layer, string curstring) 9 { 10 List<string> list = new List<string>(); 11 for (int i = 0; i < dimvalue.Count; i++) 12 { 13 for (int j = i; j < dimvalue.Count - 1; j++) 14 { 15 if (dimvalue[i].Count < dimvalue[j].Count) 16 { 17 list = dimvalue[j];//循环找出数组最多的一个集合 18 } 19 } 20 } 21 for (int i = 0; i < list.Count; i++) 22 { 23 StringBuilder bs = new StringBuilder(); 24 bs.Append(curstring); 25 for (int j = 0; j < dimvalue.Count; j++) 26 { 27 if (dimvalue[j].Count > i) 28 { 29 bs.Append(dimvalue[j][i].ToString()); 30 } 31 } 32 result.Add(bs.ToString()); 33 } 34 }
后面这个暂时还没想到更好的方法,目前暂时只能想到这样
附上最后的效果