一个遍历数据组合的函数

【一个遍历数据组合的函数】using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { char[] myArray1 ={ 'a', 'b', 'c' }; char[] myArray2 ={ 'a', 'b', 'c','d','e' }; char[] myArray3 ={ 'a', 'b', 'c' }; char[][] MyAr =new char[][]{myArray1,myArray2,myArray3}; //存放数组的数组 PrintArray(MyAr); Console.ReadKey(); } static public void PrintArray(char[][] MyAr) { //动态创建数组,存储MyAr中子字符串的长度,并设置初始值为0. int[] lengthsArr = new int[] {MyAr.Length}; int[] lowerBoundsArr = {0}; Array arydepth = Array.CreateInstance(Type.GetType("System.Int32"), lengthsArr, lowerBoundsArr); for (int i = arydepth.GetLowerBound(0); i <=arydepth.GetUpperBound(0); i++) { arydepth.SetValue(0,i); //初始化 //Console.WriteLine("arr[{0}]={1}", i, arydepth.GetValue(i)); } bool isCarry = false; //指示是否结束 for (; !isCarry; ) { //输出当前下标的组合 for (int j = 0; j < MyAr.Length; j++) { Console.Write("{0} ", MyAr[j][Convert.ToInt32(arydepth.GetValue(j))]); } Console.WriteLine(); //设置数组深度字符串,每次循环设置一个新值。采用的是从最后位向前循环的思路。 //先判断是不是进位,即判断当前深度的后一个深度是不是结束了,如果是,则将当前深度增加一 //,同时判断相加后的该深度的值是不是超过了对应的自字符串的长度,如果是,则继续进位,否 //则退出循环; //如果不是进位,看看当前深度和对应的字符串深度做比较,如果小于当前字符串深度,则深度加一, //如果等于当前字符串深度,则进位。 for (int i = MyAr.Length - 1; i >= 0; i--) { if (isCarry) { arydepth.SetValue(Convert.ToInt32(arydepth.GetValue(i)) + 1, i); isCarry = false; if (Convert.ToInt32(arydepth.GetValue(i)) > MyAr[i].Length - 1) { arydepth.SetValue(0, i); isCarry = true; continue; } else { break; } } else { if (Convert.ToInt32(arydepth.GetValue(i)) <= MyAr[i].Length - 1) { if (Convert.ToInt32(arydepth.GetValue(i)) == MyAr[i].Length - 1) { arydepth.SetValue(0, i); isCarry = true; continue; } else { arydepth.SetValue(Convert.ToInt32(arydepth.GetValue(i)) + 1, i); break; } } } }//for (int i = MyAr.Length-1; i >= 0; i--) 结束 }//for (; !isCarry; ) 结束 } } } 2. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication2 { public struct MyCarry { public int CarryNum; public bool isCarry; public MyCarry(int _cary, bool _iscary) { CarryNum = _cary; isCarry = _iscary; } }; class Program { static void Main(string[] args) { char[] myArray1 ={ 'a', 'b', 'c', 'd' }; char[] myArray2 ={ 'a', 'b', 'c', 'd' }; char[] myArray3 ={ 'a', 'b', 'c', 'd' }; char[] myArray4 ={ 'a', 'b', 'c','d'}; char[][] MyAr = new char[][] { myArray1, myArray2, myArray3, myArray4 }; //存放数组的数组 PrintArray(MyAr); Console.ReadKey(); } static public void PrintArray(char[][] MyAr) { //动态创建数组,存储MyAr中子字符串的长度,并设置初始值为0. int[] lengthsArr = new int[] { MyAr.Length }; int[] lowerBoundsArr = { 0 }; Array arydepth = Array.CreateInstance(Type.GetType("System.Int32"), lengthsArr, lowerBoundsArr); MyCarry mycarry = new MyCarry(0, false); for (int i = arydepth.GetLowerBound(0); i <= arydepth.GetUpperBound(0); i++) { arydepth.SetValue(-1, i); //Console.WriteLine("arr[{0}]={1}", i, arydepth.GetValue(i)); } arydepth.SetValue(0, arydepth.GetUpperBound(0)); int number = 0; for (; !mycarry.isCarry; ) { number++; //输出当前下标的组合 //for (int j = 0; j <= mycarry.CarryNum; j++) for (int j = mycarry.CarryNum; j >= 0; j--) { int num = MyAr.Length - 1 - j; Console.Write("{0}", arydepth.GetValue(num)); //Console.Write("{0} ", MyAr[num][Convert.ToInt32(arydepth.GetValue(num))]); } Console.WriteLine(); //设置数组深度字符串,每次循环设置一个新值。采用的是从最后位向前循环的思路。 for (int i = MyAr.Length - 1; i >= 0; i--) { if (mycarry.isCarry) { if (mycarry.CarryNum < MyAr.Length - i - 1) { mycarry.CarryNum += 1; } arydepth.SetValue(Convert.ToInt32(arydepth.GetValue(i)) + 1, i); mycarry.isCarry = false; if (Convert.ToInt32(arydepth.GetValue(i)) > MyAr[i].Length - 1) { arydepth.SetValue(0, i); mycarry.isCarry = true; continue; } else { break; } } else { if (Convert.ToInt32(arydepth.GetValue(i)) <= MyAr[i].Length - 1) { if (Convert.ToInt32(arydepth.GetValue(i)) == MyAr[i].Length - 1) { arydepth.SetValue(0, i); mycarry.isCarry = true; continue; } else { arydepth.SetValue(Convert.ToInt32(arydepth.GetValue(i)) + 1, i); break; } } } } } Console.WriteLine("总数:"+number); } } }
--------------------
程序在控制台中运行的时候不能全部显示,读者可以引用一个输出到文件的函数,保存在txt中。

    推荐阅读