如:string[] a = new string[] {"1","2","3"}
string[] b = new string[] {"One","Two","Three"}
string[] c = new string[] {"一","二","三"}
.......等 结果: 1 One 一
1 Two 一
1 Three 一
2 One 一
2 Two 一
2 Three 一
3 One 一
3 Two 一
3 Three 一
1 One 二
1 Two 二
1 Three 二
2 One 二
2 Two 二
2 Three 二
3 One 二
3 Two 二
3 Three 二
1 One 三
1 Two 三
1 Three 三
2 One 三
2 Two 三
2 Three 三
3 One 三
3 Two 三
3 Three 三
请问:数组可能是3个,4个,不等。
这个算法如何写,不胜感激.
string[] b = new string[] {"One","Two","Three"}
string[] c = new string[] {"一","二","三"}
.......等 结果: 1 One 一
1 Two 一
1 Three 一
2 One 一
2 Two 一
2 Three 一
3 One 一
3 Two 一
3 Three 一
1 One 二
1 Two 二
1 Three 二
2 One 二
2 Two 二
2 Three 二
3 One 二
3 Two 二
3 Three 二
1 One 三
1 Two 三
1 Three 三
2 One 三
2 Two 三
2 Three 三
3 One 三
3 Two 三
3 Three 三
请问:数组可能是3个,4个,不等。
这个算法如何写,不胜感激.
for(int i=0; i<arr1.length; i++){
输出 arr1[i]
for(int j=0; j<arr2.length; j++){
输出 aar2[i]
for()......
}
}
哎
{
Console.Write(a[i]);
Console.Write(" ");
for(int j=0;j<b.Length;j++)
{
Console.Write(b[i]);
Console.Write(" ");
for(int k=0;k<c.Length;k++)
{
Console.Write(c[i]);
Console.Write("\r\n");
}
}
}
{
Console.Write(a[i]);
Console.Write(" ");
for(int j=0;j<b.Length;j++)
{
Console.Write(b[j]);
Console.Write(" ");
for(int k=0;k<c.Length;k++)
{
Console.Write(c[k]);
Console.Write("\r\n");
}
}
}
string[] b = new string[] { "One", "Two", "Three" };
string[] c = new string[] { "一", "二", "三" };
string[] d = new string[a.Length * b.Length * c.Length];
for (int i = 0; i < a.Length; i++)
{
for (int j = 0; j < b.Length; j++)
{
for (int k = 0; k < c.Length; k++)
{
d[i * a.Length * b.Length + j * a.Length + k] = a[i] + " " + b[j] + " " + c[k];
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication28
{
class Program
{
static void Main(string[] args)
{ string[] a = new string[] { "1", "2", "3" };
string[] b = new string[] {"One","Two","Three"} ;
string[] c = new string[] {"一","二","三"} ;
int k = 0;
for (int h = 0; h < c.Length; h++)
{
for (int i = k; i < a.Length; i++)
{
for (int j = 0; j < b.Length; j++)
{
Console.WriteLine("{0},{1},{2}", a[i], b[j],c[h]);
}
}
}
}
}
}
就能实现不用知道数组的数量 动态遍历了
{
string[] a = new string[] { "1", "2", "3" };
string[] b = new string[] {"One","Two","Three","four"} ;
string[] c = new string[] {"一","二","三"} ;
List<string[]> al = new List<string[]>();
al.Add(a);
al.Add(b);
al.Add(c);
string[] mmm;
mmm = bianli(al);
}//在这打个断点,看看mmm
private string [] bianli(List<string[]> al)
{
if(al.Count==0)
return null;
int size = 1;
for (int i = 0; i < al.Count; i++)
{
size = size * al[i].Length;
}
string[] str = new string[size];
for (int j = 0; j < size; j++)
{
for(int m=0;m<al.Count;m++)
{
str[j] = str[j] + al[m][(j * jisuan(al, m) / size)%al[m].Length] + " ";
}
str[j]=str[j].Trim(' ');
}
return str;
}
private int jisuan(List<string[]> al,int m)
{
int result = 1;
for (int i = 0; i < al.Count; i++)
{
if (i <= m)
{
result = result * al[i].Length;
}
else
{
break;
}
}
return result;
}
static void Main(string[] args)
{
string[] arr1 = {"aa","bb","cc"};
string[] arr2 = {"11","22","33"};
string[] arr3 = {"xx","yy","zz"};
List<string[]> list = new List<string[]>();
list.Add(arr1); list.Add(arr2); list.Add(arr3);
test(list,0,""); Console.ReadLine();
}static void test(List<string[]> list, int index, string str)
{
string tempStr;
//迭代list
for (int i = 0; i < list.Count; i++)
{
//取得当前的数组
if (i == index)
{
//迭代数组
foreach (string st in list[index])
{
tempStr = str + st;
if (i < list.Count - 1)
{
test(list, i + 1, tempStr);
}
else if (i == list.Count - 1)
{
Console.Write(tempStr);
Console.WriteLine();
}
}
}
}
}