- 递归实现
int View::fibRecursively(int n)
{
int ret = 0;
if(n <= 0)
{
ret = 0;
}else if(n == 1 || n == 2)
{
ret = 1;
}else{
ret = fibRecursively(n-1) + fibRecursively(n-2);
}
return ret;
}
- 循环实现
int View::fib(int n)
{
int ret = 0;
int nextItem = 1;
if(n <= 0)
{
ret = 0;
}else{
for(int i = 1 ; i <= n ; ++i)
{
int tmp = nextItem;
nextItem += ret;
ret = tmp;
}
}
return ret;
}