C#排列组合返回结果
请问,C#编写的排列组合算法,用的是递归算法,如何把最好的组合结果返回出来,存储到一个数组中(我想返回下面代码中的"lin",但怎么弄返回都是空的)staticvoidS...
请问,C#编写的排列组合算法,用的是递归算法,如何把最好的组合结果返回出来,存储到一个数组中(我想返回下面代码中的"lin",但怎么弄返回都是空的)
static void Select(int currentIndex, int remain, string currentSelect,string [] array,out List<string[]> lin)
{
lin = new List<string[]>();
if (remain == 0)
{
string a = currentSelect.Remove(0,1);
Console.WriteLine(a);
string[] bb = a.Split(',');
lin.Add(bb);
return;
}
if (array.Length - currentIndex < remain)
{
return;
}
Select(currentIndex + 1, remain - 1, currentSelect + "," + array[currentIndex],array,out lin);
Select(currentIndex + 1, remain, currentSelect,array,out lin);
} 展开
static void Select(int currentIndex, int remain, string currentSelect,string [] array,out List<string[]> lin)
{
lin = new List<string[]>();
if (remain == 0)
{
string a = currentSelect.Remove(0,1);
Console.WriteLine(a);
string[] bb = a.Split(',');
lin.Add(bb);
return;
}
if (array.Length - currentIndex < remain)
{
return;
}
Select(currentIndex + 1, remain - 1, currentSelect + "," + array[currentIndex],array,out lin);
Select(currentIndex + 1, remain, currentSelect,array,out lin);
} 展开
2个回答
展开全部
class Program
{
/// <summary> 声明委托 </summary>
/// <param name="bb"></param>
public delegate void AcquireArray(string[] bb);
/// <summary> 定义委托变量 </summary>
static AcquireArray acquireArray;
static void Main(string[] args)
{
///赋值委托执行方法
List<string> lin = new List<string>();
acquireArray = (string[] bb) => { lin.AddRange(bb); };
Select(0, 5, "as", new string[] {"1","2","3","4","5" });
Console.WriteLine();
Console.WriteLine("下面数据为list内存储数据:");
Console.WriteLine(string.Join("\r\n", lin));
Console.ReadKey();
}
static void Select(int currentIndex, int remain, string currentSelect, string[] array)
{
if (remain == 0)
{
string a = currentSelect.Remove(0, 1);
Console.WriteLine(a);
string[] bb = a.Split(',');
///执行这个委托
acquireArray.Invoke(bb);
return;
}
if (array.Length - currentIndex < remain)
{
return;
}
Select(currentIndex + 1, remain - 1, currentSelect + "," + array[currentIndex], array);
Select(currentIndex + 1, remain, currentSelect, array);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询