【发布时间】:2023-03-29 21:37:02
【问题描述】:
我一直致力于使用回溯算法生成 3 个数字组合 到目前为止,我已经完成了以下工作:
static int a, b, c;
static void Combo(int a,int b, int c)
{
if (a != 10)
{
Combo(a, b, c);
a++;
}
if (b != 10)
{
Combo(a, b, c);
b++;
}
if(c != 10)
{
Combo(a,b,c);
c++;
}
Console.WriteLine("( {0}, {1}, {2})",a,b,c);
}
主要方法:
static void Main(string[] args)
{
Console.WriteLine("Press Any Key to Start Generating xxx number Combination");
Console.ReadKey();
Combo(0,0,0);
Console.WriteLine("Combo function has finished Successfully!");
Console.ReadKey();
}
我在 cmd 上收到一条消息“进程因 StackOverFlowException 而终止。”
我的代码有问题吗? 如果没有,你能帮我解决这个过流问题吗 注意:我只想要一个递归回溯算法;我不是在寻找任何超出我的联盟的花哨、有创意的东西。
感谢转发!
【问题讨论】:
标签: c# algorithm combinations subset backtracking