我是自學C++ Sams自學C++一天一小時和第150頁作者討論了使用斐波那契數列的遞歸函數。使用斐波那契數列的遞歸函數
他使用以下代碼:
#include <iostream>
using namespace std;
int GetFibNumber(int FibIndex)
{
if(FibIndex < 2)
return FibIndex;
else
return GetFibNumber(FibIndex - 1) + GetFibNumber(FibIndex - 2);
}
int main()
{
cout << " Enter 0 based index of desired Fibonacci Number: ";
int Index = 0;
cin >> Index;
cout << " Fibonacci number is: " << GetFibNumber(Index) << endl;
return 0;
}
是什麼具有
返回GetFibNumber之間的差值(FibIndex - 1)+ GetFibNumber(FibIndex - 2);
和
返回FibIndex - 1 + FibIndex - 2;
爲什麼你必須在它自己內部調用函數?
預先感謝您!
您能否提供一個您做過的例子,並更清楚地解釋您的問題。 –
第9行現在讀了什麼?沒有這兩個函數調用,它將不會計算斐波那契數列。 –
請看看斐波納契系列是什麼http://en.wikipedia.org/wiki/Fibonacci_number如果沒有最後一次'GetFibNumber(FibIndex - 1)+ GetFibNumber(FibIndex - 2)'計算是錯誤的。 – bkausbk