Fibonacci

Fibonacci

解法一:递归

long long Fibonacci(unsigned int n)
{
    int result[3] = {0,1,2};
    if (n <= 2)
        return result[n];
    return Fibonacci(n - 1) + Fibonacci(n - 2);
}

解法二:非递归

long long Fibonacci(unsigned int n)
{
    long long first, second, result;
    first = second = result = 1;
    if (n < 2)
 return result;
    else
    {
        while(n >= 2)
        {
            first = second;
            second = result;
            result = first + second;
            n--;
        }
 return result;
    }
}
点赞