Fibonacci Function Question

前端 未结 8 1141
春和景丽
春和景丽 2020-12-31 21:22

I was calculating the Fibonacci sequence, and stumbled across this code, which I saw a lot:

    int Fibonacci (int x)
{
    if (x<=1) {
        return 1;
         


        
8条回答
  •  难免孤独
    2020-12-31 21:47

    Or if you want to be more quick but use more memory use this.

    int *fib,n;
    void fibonaci(int n) //find firs n number fibonaci
    {
     fib= new int[n+1];
     fib[1] = fib[2] = 1;
     for(int i = 3;i<=n-2;i++)
         fib[i] = fib[i-1] + fib[i-2];
    }
    

    and for n = 10 for exemple you will have : fib[1] fib[2] fib[3] fib[4] fib[5] fib[6] fib[7] fib[8] fib[9] fib[10] 1 1 2 3 5 8 13 21 34 55``

提交回复
热议问题