计算并返回斐波那契(Fibonacci)数列中第n项的值。 Code#include<iostream>using namespace std;int Fibonacci(int);int Fib(int);int main(){ int n; cin>>n; cout<<"递归:Fibonacci数列的第"<<n<<"项是 "<<Fib(n)<<endl; cout<<"非递归:Fibonacci数列的第"<<n<<"项是 "<<Fibonacci(n)<<endl; return 0;}//非递归int Fibonacci(int n){ int fib1=1; int fib2=1; int fib3; if(n==1 || n==2) return 1; else { for(int i =3; i<=n; i++) { fib3 = fib1 + fib2; fib1 = fib2; fib2 = fib3; } return fib3; }}//递归int Fib(int n){ int fib = 1; if(n==1 || n==2) fib = 1; else fib = Fib(n-1) + Fib(n-2); return fib;} 来源:https://www.cnblogs.com/nother/archive/2009/02/11/1388243.html 标签 斐波那契 递归