为什么递归函数默认必须返回1 [英] Why recursive function must return 1 by default

查看:194
本文介绍了为什么递归函数默认必须返回1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下递归功能:

  public   int  Fibonacci( int  number)
{
if ( number == 0
return 0 < /跨度>;
else if (number == 1
return 1 ;
else
{
return 斐波那契(数字 - 2 )+ Fibonacci(number - 1 );
}

}



我的问题是:



为什么函数返回0如果我在else条件下返回0,

为什么函数返回值乘以2如果我在else条件下返回2。

所以测试场景将是:



斐波纳契(5)//何时(否则返回0)|结果:0

斐波纳契(5)//何时(否则返回1)|结果:5 //正确

斐波那契(5)//何时(否则返回2)|结果:10





我不明白为什么,以及何时发生乘法运算。



有什么解释吗?



我尝试了什么:



  public   int 斐波那契( int  number)
{
if (number == 0
return 0 ;
else if (number == 1
return 1 ;
else
{
return 斐波那契(数字 - 2 )+ Fibonacci(number - 1 );
}

}

解决方案

0和1为您提供一种方式结束的递归方法:如果测试不存在那么它就会继续调用自己,直到它耗尽堆栈空间并崩溃!

想想它的作用,并尝试在调试器。在第一次调用方法时挖一个断点,然后进入它,并仔细观察当你单步执行时会发生什么。你很快就会看到发生了什么,为什么这两个测试是必不可少的!


这不是你的代码!

首先要做的是阅读有关Fibonacci序列的参考文献斐波纳契数 - 维基百科,免费的百科全书 [ ^ ]。



引用:

为什么函数返回0如果我返回0

这是定义的一部分F0 = 0



引用:

在else条件下,

为什么函数返回如果我在else条件下返回2,则乘以2。

从1跳到2不是乘法。更仔细地阅读源代码。



引用:

所以测试场景将是:



Fibonacci(5)// when(else return 0)|结果:0

斐波纳契(5)//何时(否则返回1)|结果:5 //正确

斐波那契(5)//何时(否则返回2)|结果:10

不!

检查参考文献以获得正确的答案。



引用:

我不明白为什么,当乘法发生的时候。



有什么解释吗?

因为没有乘法!

再次阅读源代码。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]


I have the following recursive function:

public int Fibonacci(int number)
      {
          if (number == 0)
              return 0;
          else if (number == 1)
              return 1;
          else
          {
              return Fibonacci(number - 2) + Fibonacci(number - 1);
          }

      }


My questions are:

why the function returns 0 if i return 0 in the else condition,
why the function returns the value multiplied by 2 if I return 2 in the else condition.
So the test scenario will be:

Fibonacci(5) // when (else return 0) | Result: 0
Fibonacci(5) // when (else return 1) | Result: 5 // Correct
Fibonacci(5) // when (else return 2) | Result: 10


I don't understand why, and when the multiplication is happening.

Any explanation?

What I have tried:

public int Fibonacci(int number)
      {
          if (number == 0)
              return 0;
          else if (number == 1)
              return 1;
          else
          {
              return Fibonacci(number - 2) + Fibonacci(number - 1);
          }

      }

解决方案

0 and 1 provide a way for your recursive method to end: if the tests weren't there then it would just keep on calling itself until it ran out of stack space and crashed!
Think about what it does, and try running it in the debugger. Pit a breakpoint on the first call to the method, and step into it, and watch exactly what happens as you step through. You'll soon start to see what's happening, and why the two tests are essential!


This is not your code!
So first thing to do is to read a reference about the Fibonacci sequence Fibonacci number - Wikipedia, the free encyclopedia[^].

Quote:

why the function returns 0 if i return 0

This is part of the definition F0= 0

Quote:

in the else condition,
why the function returns the value multiplied by 2 if I return 2 in the else condition.

Jumping from 1 to 2 is not necessary a multiplication. read source code more carefully.

Quote:

So the test scenario will be:

Fibonacci(5) // when (else return 0) | Result: 0
Fibonacci(5) // when (else return 1) | Result: 5 // Correct
Fibonacci(5) // when (else return 2) | Result: 10

No!
Check the reference to get the right answers.

Quote:

I don't understand why, and when the multiplication is happening.

Any explanation?

Because there is no multiplication!
Read again the source code.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]


这篇关于为什么递归函数默认必须返回1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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