求用C#实现数字组合算法

DHBTONY 2008-09-01 10:17:49
现有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}.....
不需要考虑数字排列顺序,每组中的元素只能与其他组中的元素进行组合,
请问如何使用C#实现所有组合的算法!!
...全文
1001 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
youHavesi 2009-09-16
  • 打赏
  • 举报
回复
List<string> ss = new List<string>();//结果
const int NumberLen = 3;//组合长度
const int Numbers = 10;//号码数
private void fnNum(string s,int i,int d)
{
for (int n = i; n < Numbers - NumberLen + d + 1; n++)
{
if (d == NumberLen)
{
ss.Add(s + n);
}
else
{
fnNum(s + n + ",", n + 1, d + 1);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
fnNum("", 1, 1);
}
shengaiming 2009-01-30
  • 打赏
  • 举报
回复
我一直在寻找组合最快算法,不知汇编组合速度怎样?
yatobiaf 2008-09-01
  • 打赏
  • 举报
回复
这个问题的算法有且只有4楼一种,只是实现形式可能有所不同。可以用数组,也可以用arraylist,如果是C++还可以用vector,但是思想都是,循序遍历每个容器,和每个容器中的所有元素。
yatobiaf 2008-09-01
  • 打赏
  • 举报
回复
这个问题的算法有且只有4楼一种,只是实现形式可能有所不同。可以用数组,也可以用arraylist,如果是C++还可以用vector,但是思想都是,循序遍历每个容器,和每个容器中的所有元素。
yatobiaf 2008-09-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 DHBTONY 的回复:]

1,2,3,4,5;
6,7,8,9;
10,11,12;

从三组数中分别取一个数进行组合,如{1,6,10},{1,6,11},{1,6,12},{1,7,10}.....
不需要考虑数字排列顺序,每组中的元素只能与其他组中的元素进行组合,
请问如何使用C#实现所有组合的算法!!

我最终的想法实现下面的功能
若数组的个数不定,该怎么实现呢,比如说有10组数,难道要10个嵌套的循环,能否提供递归调用的算法实现数组个数不确定的数字组…
[/Quote]
那就用两个list相互嵌套啊。
List<List<int> >
或者
ArrayList<ArrayList<int> >
这样你for循环结束的条件就是i<ArrayList.size()
yatobiaf 2008-09-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ojlovecd 的回复:]
C# code
int[] a = { 1, 2, 3, 4, 5 };
int[] b = { 6, 7, 8, 9 };
int[] c = { 10, 11, 12 };
for (int i = 0; i < 5; i++)
for (int j = 0; j < 4; j++)
for (int k = 0; k < 3; k++)
Console.WriteLine(a[i] + "," + b[j] + "," + c[k]);
[/Quote]

这是最优的也是唯一的算法。
DHBTONY 2008-09-01
  • 打赏
  • 举报
回复

1,2,3,4,5;
6,7,8,9;
10,11,12;

从三组数中分别取一个数进行组合,如{1,6,10},{1,6,11},{1,6,12},{1,7,10}.....
不需要考虑数字排列顺序,每组中的元素只能与其他组中的元素进行组合,
请问如何使用C#实现所有组合的算法!!

我最终的想法实现下面的功能
若数组的个数不定,该怎么实现呢,比如说有10组数,难道要10个嵌套的循环,能否提供递归调用的算法实现数组个数不确定的数字组合算法
JaggerLee 2008-09-01
  • 打赏
  • 举报
回复
这种题没有更好的算法的
我姓区不姓区 2008-09-01
  • 打赏
  • 举报
回复

int[] a = { 1, 2, 3, 4, 5 };
int[] b = { 6, 7, 8, 9 };
int[] c = { 10, 11, 12 };
for (int i = 0; i < 5; i++)
for (int j = 0; j < 4; j++)
for (int k = 0; k < 3; k++)
Console.WriteLine(a[i] + "," + b[j] + "," + c[k]);
haonanzhao 2008-09-01
  • 打赏
  • 举报
回复
3循环是比较低的,如果用递归,时间复杂度会更高,不过代码更通用。
Fioit 2008-09-01
  • 打赏
  • 举报
回复
三层循环。有时间复杂度更低的算法吗?
ybhcolin 2008-09-01
  • 打赏
  • 举报
回复
循环取,三个for语句
W422080367 2008-09-01
  • 打赏
  • 举报
回复
你是对N个数组进行操作。先把这N个一维数组合并成一个2为数组;然后进行操作

public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{
int[][] aa = new int[4][];
int[] a = { 1, 2, 3, 4, 5 };
int[] b = { 6, 7, 8, 9 };
int[] c = { 10, 11, 12 };
int[] d ={ 13, 14 };
aa[0] = a;
aa[1] = b;
aa[2] = c;
aa[3] = d;

myClass ff = new myClass(aa);
ff.mySelf();
ArrayList dataSource = ff._firstList;

for (int i = 0; i < dataSource.Count; i++)
listBox1.Items.Add(dataSource[i].ToString());
}

}
class myClass
{
int[][] intList;
ArrayList firstList=new ArrayList();
public ArrayList _firstList
{
get
{
return this.firstList;
}
}
public myClass(int[][] intList)
{
this.intList = intList;
firstList.AddRange(intList[0]);
}

public void mySelf()
{
for (int i = 1; i < intList.Length;i++ )
Display(intList[i]);
}
string strCount;
private void Display(int[] list)
{
strCount = string.Empty;
for (int i = 0; i < firstList.Count; i++)
{
for (int j = 0; j < list.Length; j++)
{
strCount += firstList[i].ToString() + list[j].ToString() + "&";
}
}
firstList.Clear();
firstList.AddRange(strCount.Split('&'));
firstList.RemoveAt(firstList.Count - 1);
}
}


代码经过测试,好用。
只要是2为数组,就会把每个一维数组相互进行组合。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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