该函数的第一次递归调用会是什么样子?

发布于 2025-01-21 04:01:02 字数 282 浏览 0 评论 0原文

int Fun(int m, int n)
{

    if(n==0)
  {

    return n + 2;

  }

    return Fun(n-1, m-1) + Fun(m-1,n-1) + 1;

}

对于此功能,我完全失去了第一种情况的外观。我不明白为什么该函数有两个参数,以及为什么我们只在基本情况下返回1个参数。解决这个问题的过程是什么?您想向我解释的任何输入都很好(3,3)。虽然,现在我在考虑一个功能,如果其中一个输入比另一个输入小(3,2)或(2,3)会怎样?

int Fun(int m, int n)
{

    if(n==0)
  {

    return n + 2;

  }

    return Fun(n-1, m-1) + Fun(m-1,n-1) + 1;

}

I'm completely lost as to what the 1st case would visually look like for this function. I don't understand why the function has two parameters and why we only return 1 parameter at the end with our base case. What would be the process to work this out? Any input you want to use to explain to me is fine I was trying (3,3). Although, now that I'm thinking about it how would this function look like if one of the inputs was smaller than the other like (3,2) or (2,3)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软甜啾 2025-01-28 04:01:02

请注意,返回n + 2;简化为返回2;

该函数需要两个参数(参数)并返回一个值。这就像添加您在学校第一年所学的两个数字的操作一样。

是否fun(n -1,m -1)fun(m -1,n -1)之前均未由C ++标准指定。因此,我无法告诉您第一个递归电话会是什么样。这使编译器在做出优化选择方面具有更大的自由度。当然,称为功能的顺序对最终结果没有影响。

分析特定情况下发生的情况的最佳方法是使用线路调试器使用一条线。

Note that return n + 2; simplifies to return 2;.

The function takes two arguments (parameters) and returns a single value. That's like the operation of adding two numbers that you were taught in your first year at school.

Whether or not Fun(n - 1, m - 1) is called before Fun(m - 1, n - 1) is not specified by the C++ standard. So I can't tell you what the first recursive call will look like. This gives compilers more freedom in making optimisation choices. Of course the order in which the functions are called has no effect on the eventual result.

The best way of analysing what happens in your particular case is to use a line by line debugger.

¢好甜 2025-01-28 04:01:02

递归功能没有什么特别的 - 它们的工作原理完全像非恢复功能。

fun(3,3)做到这一点:

if(3==0)
{
    return 3 + 2;
}
return Fun(2, 2) + Fun(2, 2) + 1;

它需要fun(2,2)的值,

if(2==0)
{
    return 2 + 2;
}
return Fun(1, 1) + Fun(1, 1) + 1;

这需要fun(1,1 ),do

if(1==0)
{
    return 1 + 2;
}
return Fun(0, 0) + Fun(0, 0) + 1;

and and fun(0,0)确实

if(0==0)
{
    return 0 + 2;
}
return Fun(-1, -1) + Fun(-1, -1) + 1;

返回2,因为条件是正确的。

因此,fun(1,1)将做

return 2 + 2 + 1;

5,而fun(2,2)将做

return 5 + 5 + 1;

11,fun(3,3 )将做

return 11 + 11 + 1;

23。

我敢肯定,您可以自己处理其他示例。

There is nothing special about recursive functions - they work exactly like non-recursive functions.

Fun(3,3) does this:

if(3==0)
{
    return 3 + 2;
}
return Fun(2, 2) + Fun(2, 2) + 1;

It needs the value of Fun(2,2), which does this:

if(2==0)
{
    return 2 + 2;
}
return Fun(1, 1) + Fun(1, 1) + 1;

And that needs Fun(1,1), which does

if(1==0)
{
    return 1 + 2;
}
return Fun(0, 0) + Fun(0, 0) + 1;

and Fun(0,0) does

if(0==0)
{
    return 0 + 2;
}
return Fun(-1, -1) + Fun(-1, -1) + 1;

which returns 2 since the condition is true.

So, Fun(1, 1) will do

return 2 + 2 + 1;

which is 5, and Fun(2,2) will do

return 5 + 5 + 1;

which is 11, and Fun(3,3) will do

return 11 + 11 + 1;

which is 23.

I'm sure you can work through other examples on your own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文