简单问题,关于数组遍历组合
假设有14个3维数组
1,2,3,
4,5,6
7,8,9,
3,4,7,
6,4,6
x,x,x
x,x,x
x,x,x........
要如何用数组1;1,2,3分别去和剩下的13个数组种的每一个数进行组合,最后得到一个14位的数?
------解决方案--------------------用回朔法写一个:(生般硬套网上人家现成的,别见笑!对几个几维都行.)
class A
{
private int Count = 0; //存放记录数.
static void Main(string[] args)
{
char[,] c ={ { '1 ', '2 ', '3 ' }, { '3 ', '2 ', '1 ' }, { '4 ', '5 ', '6 ' }, { '7 ', '8 ', '9 ' }, { 'a ', 'b ', 'c ' }, { 'd ', 'e ', 'f ' }, { 'g ', 'h ', 'i ' }, { 'j ', 'k ', 'l ' } };
A newA = new A();
int n = c.GetLength(0);
newA.combine(c,n);
Console.WriteLine(newA.Count);
Console.Read();
}
private void combine(char[,] c, int n)
{
int[] r = new int[n];
for (int i = 0; i < n - 1; i++)
{
r[i] = 0;
}
bool f = true;
int k = n-1;
while (k > = 0)
{
if (f)
{
print(c, r);//输出结果
f = false;
Count++;
}
r[k]++;
if (r[k] > =c.GetLength(1))
{
k--;
continue;
}
if (k < n - 1)
{
r[++k] = -1;
continue;
}
if (k == n - 1)
{
f = true;
}
}
}
private void print(char[,] c, int[] r)
{
for (int i = 0; i < r.Length; i++)
{
Console.Write(c[i,r[i]]);
}
Console.WriteLine();
}
}