在 C++ 中编写递归函数的最佳方法?
问题
我想知道这是否是实现可变深度递归的可行方法,以便我可以在每一步运行一个函数以及描述问题的任何更好/其他解决方案。
描述
假设我希望有一个函数可以以模式填充数组x,y,x,y,x,y
其中 x 和 y 是由某种算法定义的变量
和 x,y,z,x,y,z 其中 x、y 和 z 是由相同算法定义的变量。
对于所有数量的变量都应该继续这样。这是实施它的可行方法吗?
void recurse_n(int n)
{
while(n > 0)
{
--n;
recurse_n(n);
n = 0;
// Use algorithm here
}
}
编辑:删除了前面提到的不正确的返回类型。脑放屁。
Question
I wish to know if this is a viable way to implement variable depth recursion so that I can run a function at each step and any better/other solutions to the description problem.
Description
Suppose I wish to have a function that fills an array either in patternx,y,x,y,x,y
where x and y are variables defined by some algorithm
and x,y,z,x,y,z
where x, y and z are variables defined by the same algorithm.
This should continue for all number of variables. Is this a viable way to implement it.
void recurse_n(int n)
{
while(n > 0)
{
--n;
recurse_n(n);
n = 0;
// Use algorithm here
}
}
EDIT: Removed the incorrect return type previously referred to. Brainfart.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因此,根据您的评论,您想知道设置递归函数的最佳方法。你所做的将会起作用,但是它是令人费解的并且有点令人困惑。我会做的简化是:
这将使您更容易看到正在发生的事情。我要补充的一件事是,您可能想在调用 recurse_n 之前执行算法操作,但这一切都取决于您的算法正在做什么。
如果您考虑一下,按照我编写的方式,在执行任何算法工作之前,它将递归直到 n 小于或等于 0。您可能想要执行算法工作然后递归。
So, based on your comment you want to know the best way to set up a recursive function. What you have done will work, but it is convoluted and slightly confusing. What I would do to simplify it is:
That will make it easier to see what is going on. One thing I would add though is that you might want to do the algorithm stuff before the call to recurse_n, but that all depends on what your algorithm is doing.
If you think about it, the way I have written it, it will recurse until n is less than or equal to 0 before doing any of the algorithm work. It might be that you want to do the algorithm work then recurse.
首先,使用 std::vector 和循环(我假设 x()、y() 和 z() 返回您需要的 int,您也可以在那里使用向量存储值):
这更像 C++,并且比递归函数快得多。另外:存储 x、y 和 z 值,以便相应的算法仅执行一次。
First of all, use a std::vector and a loop (I'm assuming x(), y() and z() return the
int
s you need, you could also use a vector there to store the values):This is more C++-ish and much faster than a recursive function. Also: store x, y, and z values so that the corresponding algorithm only executes once.
我认为这不是一种可行的方法,因为:让
T(n)
表示函数的运行时间(取决于输入参数n
)。基本情况
n=0
产生以下运行时间:T(0)=c
,即某个恒定的运行时间c
。现在您可以为运行时间定义一个递归公式,其中
n>0
:T(n)=sum(i = 0 to n-1: T(i))
。如果你解这个方程,你会得到
T(n)=O(2^n)
,这意味着你的算法是指数的,这意味着它在实践中不易处理。I do not think this is a viable way to do it, since: Let
T(n)
denote the running time of your function (dependent on input parametern
).The base case
n=0
yields following running time:T(0)=c
, i.e. some constant runtimec
.Now you can define a recursive formula for the running time where
n>0
:T(n)=sum(i = 0 to n-1: T(i))
.If you solve this equation you get that
T(n)=O(2^n)
, which means that your algorithm is exponential, which means that it is not tractable in practice.我对这个问题有点不清楚,但听起来你有一组变量
a, b, c, ..., z
并且你想填充一个数组,因此它包含>a、b、c、...、z、a、b、c、...、z、a、...
。如果是这样,最简单的方法可能是将源变量放入自己的一次性数组a, b, c, ..., z
中,然后将其memcpy
放入目标数组直到装满为止I'm a little unclear on the question, but it sounds like you have a set of variables
a, b, c, ..., z
and you want to fill an array so it containsa, b, c, ..., z, a, b, c, ..., z, a, ...
. If so, it's probably simplest to put the source variables in their own one-pass arraya, b, c, ..., z
andmemcpy
it to the destination array until it's filledJimDaniel 是对的,这里的递归有点矫枉过正。您没有从函数返回任何内容,看起来您只使用“n”来控制递归次数。使用简单的 for 循环会更清晰+更高效。
JimDaniel is right, the recursion here is an overkill. You're not returning anything from the function and it looks like you're only using "n" to control the number of recursions. Using a simple for-loop would be much clearer + more efficient.
如果您的算法满足以下要求,则递归填充数组是一个有效的(甚至是最好的)选项:
n
处的值取决于至少一些位于它之前的值的值n
处的值。符合这些要求的一个示例是斐波那契数。因为如果不先确定所有较早的数字,就无法确定第 n 个数字(尽管存在一些快捷方式)。
不符合这些要求的一个示例是用索引的平方填充的数组,其中位置
n
处的值只是n^2
。最后,如果可能的话,我建议您根据 DaveJohnston 对您问题的回答中的模式重写您的函数。
Recursion to fill an array is a valid (even the best) option if your algorithm meets these requirements:
n
depends on the values of at least some of the values that came before itn
without first determining the earlier values that it depends on.An example that fits these requirements are the fibonacci numbers. Because you can't determine the n-th number without first determining all earlier numbers (though some shortcuts exist).
An example that doesn't fit these requirements is an array that is filled with the square of the index, where the value at position
n
is justn^2
.Finally, I'd suggest you rewrite your function according to the pattern in DaveJohnston's answer to your question if possible.