# 优化M个数中取N个数的算法

0

/// <param name="currentIndex">当前数组索引</param>
/// <param name="remain">需取的个数</param>
/// <param name="currentSelect">生成当前组合的字符串集合</param>
/// <param name="Items">M个数集合</param>
static void comb(int currentIndex, int remain, string currentSelect, string[] Items)
{
if (remain == 0)
{
//当此行不需取数时，列出此行取的N个数
strResult += currentSelect + ";";
return;
}

if (Items.Length - currentIndex < remain)
return;
//递归取数
comb(currentIndex + 1, remain - 1, currentSelect + "," + Items[currentIndex], Items);
comb(currentIndex + 1, remain, currentSelect, Items);
}

0
```static void Main(string[] args)
{
for (int i = 0; i < Items.Length; i++)
{
t.Start(i);
}

Console.Write(strResult.Length);
}
static string[] Items = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10","11","12","13","14","15","16","17","18",
"19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36"};
static void DoWork(object o)
{
comb(int.Parse(o.ToString()), 18, "", Items);
}

static string strResult = "";
/// <param name="currentIndex">当前数组索引</param>
/// <param name="remain">需取的个数</param>
/// <param name="currentSelect">生成当前组合的字符串集合</param>
/// <param name="Items">M个数集合</param>
static void comb(int currentIndex, int remain, string currentSelect, string[] Items)
{
if (Items.Length - currentIndex < remain)
return;

if (remain == 0)
{
//当此行不需取数时，列出此行取的N个数
strResult += currentSelect + ";";
return;
}

//递归取数
comb(currentIndex + 1, remain - 1, currentSelect + "," + Items[currentIndex], Items);
}```

36的18次方等于 1.031442479849054e+28

0

Alvin | 园豆：828 (小虾三级) | 2013-02-21 11:48
0

http://bbs.csdn.net/topics/300190422

Rookier | 园豆：652 (小虾三级) | 2013-02-21 17:59

您需要登录以后才能回答，未注册用户请先注册