您的位置:首页 > 其它

斐波那契数列

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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: