求用C#实现多个数组的组合算法。

vexfj 2010-01-05 04:14:15
现有3组数
1,2,3,4,5;
6,7,8,9;
10,11,12;

从三组数中分别取一个数进行组合,如{1,6,10},{1,6,11},{1,6,12},{1,7,10}.....
不需要考虑数字排列顺序,每组中的元素只能与其他组中的元素进行组合,

据我了解3组数可以用3个for循环得到组合,现在的问题是如果有n个数组的话,是否有通用的算法解决呢?希望给出代码,谢谢。。
...全文
959 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
vexfj 2010-01-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liherun 的回复:]
怎么不符合?
[/Quote]
其实我是想实现这个功能购物网站的商品规格,但想了很久还是不知如何实现呀。
vexfj 2010-01-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xianfajushi 的回复:]
写一个组合函数N次调用
[/Quote]

能给出一个这样的函数吗?
  • 打赏
  • 举报
回复
写一个组合函数N次调用
sun_li_shu 2010-01-05
  • 打赏
  • 举报
回复
学习
Imstroy 2010-01-05
  • 打赏
  • 举报
回复
object[] xx = Do(new object[] { 1, 2, 3 }, new object[] { 4, 5, 6 }, new object[] { 7, 8, 9 }……);
Imstroy 2010-01-05
  • 打赏
  • 举报
回复
liherun 算法的 改进版

public static object[] Do(params object[] param)
{
if (param == null) { return null; }
List<object[]> LO = new List<object[]>();
int sum = 1;
int n = 0;
foreach (object o in param)
{
if (o is object[])
{
object[] _o = (object[])o;
LO.Add(_o);
sum *= _o.Length;
n++;
}
}
object[] __o = new object[sum];
for (int i = 0; i < sum; i++)
{
object[] _o = new object[n];
for (int j = 0; j < n; j++)
{
object[] o = LO[j];
_o[j] = o[(i * Index(LO, j) / sum) % o.Length];
}
__o[i] = _o;
}
return __o;
}

private static int Index(List<object[]> LO, int lindex)
{
int n = 1;
for (int i = 0; i < LO.Count; i++)
{
if (i <= lindex)
{
n = n * LO[i].Length;
}
else
{
break;
}
}
return n;
}
liherun 2010-01-05
  • 打赏
  • 举报
回复
怎么不符合?
vexfj 2010-01-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liherun 的回复:]
C# codeprivatevoid button4_Click(object sender, EventArgs e)
{string[] a=newstring[] {"1","2","3" };string[] b=newstring[] {"One","Two","Three","four"} ;string[] c=newstring[] {"一","二","三"} ;
?-
[/Quote]

这个好像不太符合吧。
liherun 2010-01-05
  • 打赏
  • 举报
回复
private void button4_Click(object sender, EventArgs e)
{
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;
}
yuanlan1818 2010-01-05
  • 打赏
  • 举报
回复
nnnnnnnnnnnnnonooooooooooooooooooo

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧