1. 程式人生 > >多數組組合笛卡爾積算法

多數組組合笛卡爾積算法

length 卡爾 ring private == mmm null 數組組合 lis

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;
}

調用方式:

string[] a = new string[] { "1", "2", "3" };
string[] b = new string[] { "2", "3", "4", "5" };
string[] c = new string[] { "2", "3", "9" };

List<string[]> al = new List<string[]>(){ a,b,c };

string[] mmm= bianli(al);

多數組組合笛卡爾積算法