[ create a new paste ] login | about

Link: http://codepad.org/HqQg55Zv    [ raw code | output | fork ]

yhl20001210 - C++, pasted on Aug 25:
//17_fibonacci.cpp
//To calculate the n-th number of the Fibonacci sequence. - Using memorized search, without a vector but a GLOBAL array

#include <iostream>

using std::cin;
using std::cout;

const int MAXN=(46+4);
int mem[MAXN]={0};

int fbnci(int n);

int main() {
    int n=19;
    //cin>>n;
    cout<<fbnci(n)<<"\n";
    return 0;
}

int fbnci(int n) {
    if (mem[n]!=0)
        return mem[n];
    else
        return mem[n]=(n==0||n==1)?1:(fbnci(n-1)+fbnci(n-2));
}


Output:
1
6765


Create a new paste based on this one


Comments: