我应该如何去了解这个递归没有试错解 [英] How should I go about solving this recursion without trial and error

查看:154
本文介绍了我应该如何去了解这个递归没有试错解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);

可以简化为:

can be simplified to:

x = x - 1;
int y = x + sum_down(x) + sum_down(x);
return y;

其可以简化为:

which can be simplified to:

int y = (x-1) + sum_down(x-1) + sum_down(x-1);
return y;

其可以简化为:

which can be simplified to:

return (x-1) + 2*sum_down(x-1);

将在数学形式,

Put in mathematical form,

f(N) = (N-1) + 2*f(N-1)

与递归终止时, N 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆