Fibonacci Sequence(斐波那契数列递归)
2018-02-19 20:56
190 查看
1、question:find The nth number offibonacci sequence In mathematics, the Fibonaccinumbers are the numbers in the following integer sequence, calledthe Fibonacci sequence, and characterized by the fact that everynumber after the first two is the sum of the two preceding ones:
1 1 2 3 5 8 13 21 34 55 89 144.....
2、code:
#include<iostream>
#include<cstdio>
using namespace std;
int fib(int a)
{
if(a==0) return 0;
if(a==1) return 1;
else
{
return fib(a-1)+fib(a-2);
}
}
int n;
int main()
{
cin>>n;
cout<<fib(n);
return 0;
}
3、running
1 1 2 3 5 8 13 21 34 55 89 144.....
2、code:
#include<iostream>
#include<cstdio>
using namespace std;
int fib(int a)
{
if(a==0) return 0;
if(a==1) return 1;
else
{
return fib(a-1)+fib(a-2);
}
}
int n;
int main()
{
cin>>n;
cout<<fib(n);
return 0;
}
3、running
相关文章推荐
- 斐波那契数列非递归计算
- c#斐波那契数列(Fibonacci)(递归,非递归)实现代码
- 用递归的方法计算斐波那契数列的通项f(n),已知f1 = 1 ,f2 = 1,以后每项都是前两项的和。
- 数据结构(C#)--斐波那契数列的递归实现方法
- 斐波那契数列(迭代和递归)
- 剑指offer面试题9——矩形框覆盖(递归改循环,斐波那契数列)
- 斐波那契数列递归实现(Java)
- 用递归和迭代分别处理斐波那契数列
- 使用递归和数组求斐波那契数列
- 用for循环和递归调用写出1~N的斐波那契数列的和 和第N位的数
- 函数递归计算斐波那契数列前40项和
- 递归—回文&斐波那契数列
- DP思想在斐波那契数列递归求解中的应用
- php处理斐波那契数列非递归方法
- JavaScript刷数据结构和算法——斐波那契数列的表示(递归,非递归)
- JavaScript 递归实现 阶乘 和 斐波那契数列
- 第11周项目--递归输出Fibnacci斐波那契数列
- JAVA递归与非递归实现斐波那契数列
- 递归求斐波那契数列
- 从小猴摘桃子问题引发的斐波那契数列与递归的思考