# 二维数组 遍历路径问题

0

<P><FONT face=Verdana>一个二维数组内的数据如下排列<BR>1a 1b&nbsp;&nbsp; null null null null<BR>2a null null null null null<BR>3a 3b&nbsp;&nbsp; null null null null<BR>4a null null null null null<BR>5a null null null null null<BR>6a null null null null null<BR>7a 7b&nbsp;&nbsp; null null null null<BR>如何用算法实现的得到如下的几个排列？<BR>1a-2a-3a-4a-5a-6a-7a<BR>1a-2a-3b-4a-5a-6a-7a<BR>1a-2a-3a-4a-5a-6a-7b<BR>1a-2a-3b-4a-5a-6a-7b<BR>1b-2a-3a-4a-5a-6a-7a<BR>1b-2a-3b-4a-5a-6a-7a<BR>1b-2a-3a-4a-5a-6a-7b<BR>1b-2a-3b-4a-5a-6a-7b</FONT></P>
gguowang | 初学一级 | 园豆：185

0
7层循环？

0
class Program { static void Main(string[] args) { string[][] elements = new string[][]{ new string[]{"1a","1b"}, new string[]{"2a"}, new string[]{"3a","3b"}, new string[]{"4a"}, new string[]{"5a"}, new string[]{"6a"}, new string[]{"7a", "7b"}, }; //算出组合总个数 int total = 1; for (int x = 0; x < elements.Length; x++) total *= elements[x].Length; //创建存放组合的临时数组 string[] row = new string[elements.Length]; //编历生成组合 for (int i = 0; i < total; i++) { int current = 0, next = i; for (int j = 0; j < row.Length; j++) { current = next; int max = elements[j].Length; if (current >= max) //当下标溢出时进位 { next = current / max; current = current % max; } else { next = 0; } row[j] = elements[j][current]; } Console.WriteLine(string.Join("-", row)); } Console.Read(); } }
Klesh Wong | 园豆：780 (小虾三级) | 2008-03-17 11:47

您需要登录以后才能回答，未注册用户请先注册