您的位置:首页 > 编程语言 > C#

C# 有放回(重复)组合算法

2013-06-02 22:04 295 查看
static List<string> GetAllZuhe(List<string> list, int n)
{
if (n <= 1)
{
return list;
}
else
{
List<string> rl = new List<string>();
foreach (string x in list)
{
foreach (string y in GetAllZuhe(list, n - 1))
{
rl.Add(x + y);
}
}
return rl;
}
}
}
static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("1");
list.Add("2");
list.Add("3");
list.Add("4");
List<string> allList=GetAllZuhe(list,3 );
Console.WriteLine(allList.Count+ "\n");
bool flag = true;
foreach (string s in allList)
{
if (flag)
Console.Write(s+" ");
else
Console.Write(s + "\n");
flag = !flag;
}
Console.ReadLine();
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: