斐波那契数列
2015-09-25 07:41
232 查看
问题描述:大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。
代码如下:
class Solution {
public:
int Fibonacci(int n) {
if(n==0)
{
return 0;
}
if(n==1)
{
return 1;
}
int first=0;
int second=1;
int result=0;
int i=2;
while(i<=n)
{
result=first+second;
first=second;
second=result;
i++;
}
return result;
}
};
代码如下:
class Solution {
public:
int Fibonacci(int n) {
if(n==0)
{
return 0;
}
if(n==1)
{
return 1;
}
int first=0;
int second=1;
int result=0;
int i=2;
while(i<=n)
{
result=first+second;
first=second;
second=result;
i++;
}
return result;
}
};
相关文章推荐
- 数据结构与算法分析:哈希表
- TouchAndGuest触摸事件和手势
- Learning Bluemix 有新 Logo 了
- c++课堂
- 谷歌公布放弃 RC4 和 SSLv3 的详细计划
- C++ 多态(一)
- 世界各国时区表 -- 与北京时差
- javaWebSerivice学习篇3-WSDL文档分析
- WPF如何实现一款类似360安全卫士界面的程序?(共享源码!)
- 页头--page-header
- 理理“事务”(下)
- CentOS-6.5在线安装docker-1.7教程(升级内核)
- 巨幕: jumbotron
- 使用gdb watch调试代码
- mysql failed to open file
- Sort Colors
- iOS-截屏
- iOS-图片剪裁
- CentOS-6.5在线安装docker-1.7教程
- iOS-图片水印