我应该如何解决这个递归没有试验和错误 [英] How should I go about solving this recursion without trial and error
本文介绍了我应该如何解决这个递归没有试验和错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int sum_down(int x)
{
if (x >= 0)
{
x = x - 1;
int y = x + sum_down(x);
return y + sum_down(x);
}
else
{
return 1;
}
}
参数x的最小整数值是什么,所以返回的值大于1.000.000?
What is this smallest integer value of the parameter x, so that the returned value is greater than 1.000.000 ?
现在我只是通过反复试验,因为这个问题是通过纸张格式提出的。我不认为我会有足够的时间来做试验和错误。问题是,你们如何快速可视化这些,使得它可以很容易地解决。
Right now I am just doing it by trial and error and since this question is asked via a paper format. I don't think I will have enough time to do trial and error. Question is, how do you guys visualise this quickly such that it can be solved easily. Thanks guys and I am new to programming so thanks in advance!
推荐答案
递归逻辑:
x = x - 1;
int y = x + sum_down(x);
return y + sum_down(x);
可以简化为:
x = x - 1;
int y = x + sum_down(x) + sum_down(x);
return y;
可以简化为:
int y = (x-1) + sum_down(x-1) + sum_down(x-1);
return y;
可以简化为:
return (x-1) + 2*sum_down(x-1);
以数学形式表示,
f(N) = (N-1) + 2*f(N-1)
b $ b
当 N
为 -1
时,递归终止。 f(-1)
= 1
。
with the recursion terminating when N
is -1
. f(-1)
= 1
.
,
f(0) = -1 + 2*1 = 1
f(1) = 0 + 2*1 = 2
f(2) = 1 + 2*2 = 5
...
f(18) = 17 + 2*f(17) = 524269
f(19) = 18 + 2*524269 = 1048556
这篇关于我应该如何解决这个递归没有试验和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文