剑指offer——斐列那契数列(递归)
2016-07-03 14:11
330 查看
斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368。
这个数列从第2项开始,每一项都等于前两项之和。
“test.c”
这个数列从第2项开始,每一项都等于前两项之和。
“test.c”
<span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> using namespace std; int Fibonacci(int n) { if (n > 1) { return Fibonacci(n-1)+Fibonacci(n-2); } else if (n == 1) { return 1; } else//n == 0 { return 0; } } void test() { int n = 0; cin>>n; int total = 0; for (int i = 0;i <= n;i++) { total += Fibonacci(i); cout<<"Fibonacci("<<i<<") = "<<Fibonacci(i)<<endl; } cout<<"total = "<<total<<endl; } int main() { test(); system("pause"); return 0; }</span>
相关文章推荐
- bootstrap
- ES6入门 —— Class和Module
- ES6之——变量的解构赋值
- ES6入门——Iterator和for...of循环
- JS 中如何拼凑html
- ES6入门——let和const命令
- ES6入门—— ECMAScript简介
- MANIFEST.MF的用途(转载)
- JS 数据类型转换
- WebStorm NodeJS
- js window.open 打开新窗体 参数设置
- javascript字符操作大全
- ajax返回json时,js获取类型,是字符串类型
- javaScript--01 HTML 下 JavaScript简介
- JavaScript 日期处理类库
- 自定义JSTL标签和函数库
- node版本管理 n和nvm说明
- JavaScript--01 HTML 上 HTML简介
- h5+css 菜单导航---改变宽度
- CSS3鼠标滑过动画线条边框特效