数字组合不重复的算法
时间:2010-11-09 来源:chenping2008
具体的算法如下:(用递归的方法来解答的)
Int32[] num = { 1, 2, 3 ,4};
IList<Int32> lists = num.ToList<Int32>();
IList<Int32> results = new List<Int32>();
for (Int32 i = 0; i < num.Length; i++)
{
results.Clear();
results.Add(num[i]);
lists.Remove(num[i]);
DoOthers(lists,results);
lists = num.ToList<Int32>();
Console.WriteLine();
}
Console.Read();
private static void DoOthers(IList<int> lists,IList<Int32> results)
{
if (lists.Count == 0)
{
foreach (int item in results)
{
Console.Write(item.ToString() + " ");
}
Console.WriteLine();
return;
}
else
{
Int32[] temp = lists.ToArray<Int32>();
for (Int32 i = 0; i < temp.Length; i++)
{
results.Add(temp[i]);
lists.Remove(temp[i]);
DoOthers(lists, results);
lists.Add(temp[i]);
results.Remove(temp[i]);
}
}
}