【发布时间】:2018-03-10 09:22:45
【问题描述】:
(我的英文不太好,所以表达可能不太清楚正确。)
我想通过循环和堆栈来模拟递归。
我的目标不是关于如何提高性能,因为解决斐波那契的原始递归方法也非常无效。并且模拟几乎不可能有更好的性能。我想知道如何将递归更改为循环和堆栈。
求解斐波那契数列的递归版本(只是递归的一个例子)。很简单
int fib(int i)
{
if (i == 0)
return 0;
if (i == 1)
return 1;
return fib(i - 1) + fib(i - 2);
}
这是我的递归模拟
int fib2(int a)
{
Stack *stack = NULL;
int temp = -1;
int i[3] = {a, -1, -1};
stack = stack_push(stack, i);
while(!stack_empty(stack))
{
int *top = stack_top(stack);
if (temp != -1 && top[1] == -1)
{
top[1] = temp;
temp = -1;
}
else if(temp != -1 && top[2] == -1)
{
top[2] = temp;
temp = -1;
}
if (top[0] == 0)
{
stack = stack_pop(stack);
temp = 0;
continue;
}
else if(top[0] == 1)
{
stack = stack_pop(stack);
temp = 1;
continue;
}
else
{
int j[3] = {top[0], -1, -1};
if (top[1] == -1)
{
j[0] = top[0] - 1;
stack = stack_push(stack, j);
}
else if (top[2] == -1)
{
j[0] = top[0] - 2;
stack = stack_push(stack, j);
}
else
{
temp = top[1] + top[2];
stack = stack_pop(stack);
}
continue;
}
}
return temp;
}
栈是用链表实现的,相关功能很简单。 它运行良好,但我认为我这样做的方式太慢且太难了。
我只是想知道如何才能更轻松地做到这一点? (不是用循环来求解斐波那契而是模拟递归)
我真正关心的是如何处理多个递归函数调用。
对于 1 个这样的函数调用。
int sum(int i)
{
if (i == 0)
return 0;
return i + sum(i - 1);
}
使用循环和堆栈很容易模拟。也很有效。
int sum2(int a)
{
Stack *stack = NULL;
while (a > 0)
{
stack = stack_push(stack, a);
a--;
}
int i = 0;
while (!stack_empty(stack))
{
i += stack_top(stack);
stack = stack_pop(stack);
}
return i;
}
但是对于超过 1 个电话,我所知道的只是使用这种愚蠢的方式来做(放一个 -1 作为标志)。
【问题讨论】:
-
当然比直接使用递归要慢,甚至比使用循环还要慢。你期待什么?
-
我希望有一种正确的方法来模拟递归,因为 c 中的堆栈大小在默认情况下是有限的。因此,我想改用堆。这只是一种练习。
-
量化“它太迟钝和无效”与什么相比?需要改进多少?没有看到
Stack, stack_pop(), stack_pop(), ...,这个帖子是不必要的模糊。 -
如果可行,请坚持使用您的解决方案。真的有那么慢吗?