日期:2014-05-18 浏览次数:21059 次
List<string> allResult = new List<string>(); string[] a = new string[] { "1", "2", "3" }; string[] b = new string[] { "4"}; string[] c = new string[] { "8", "9" }; List<string[]> All = new List<string[]>(); All.Add(a); All.Add(b); All.Add(c); Descartes(All, 0, allResult, string.Empty, "&"); foreach (var item in allResult) { Response.Write(item + "<br />"); }
private static string Descartes(List<string[]> ListArr, int Count, List<string> Result, string SourceData, string MergeChar) { string strTemp = SourceData; //获取当前数组 string[] strArr = ListArr[Count]; //循环当前数组 foreach (var item in strArr) { if (Count + 1 < ListArr.Count) strTemp += Descartes(ListArr, Count + 1, Result, item + MergeChar + SourceData, MergeChar); Result.Add(SourceData + item); } return strTemp; }
4&1&8 4&1&9 1&4 1 4&2&8 4&2&9 2&4 2 4&3&8 4&3&9 3&4 3
1 2 3 4 8 9 1&4 1&8 1&9 2&4 2&8 2&9 3&4 3&8 3&9 1&4&8 1&4&9 2&4&8 2&4&9 3&4&8 3&4&9 4&8 4&9
private static string Descartes(List<string[]> ListArr, int Count, List<string> Result, string SourceData, string MergeChar) { if (SourceData == "1") { Console.WriteLine(""); } //获取当前数组 string[] strArr = ListArr[Count]; //循环当前数组 for (int i = Count; i < ListArr.Count; i++) { string[] tempStrArr = ListArr[i]; foreach (string tempStr in tempStrArr) { string strTemp = SourceData+tempStr; Result.Add(strTemp); if (i < ListArr.Count - 1) { Descartes(ListArr, i + 1, Result, strTemp, MergeChar); } } } return ""; }