felipe tonello
By: a guest | Jan 29th, 2009 | Syntax:
C++ | Size: 0.52 KB | Hits: 1,187 | Expires: Never
#include <iostream>
#include <cstdlib>
namespace Fibonacci {
int *memory;
int F(int n)
{
// int inicializa como 0
if (memory[n] != 0) return memory[n];
int aux = n;
if (n < 0) return 0;
if (n > 1) aux = F(n-1) + F(n-2);
return memory[n] = aux;
}
}
int main(int argc, char *argv[])
{
int n = atoi(argv[1]);
Fibonacci::memory = new int[n+1];
std::cout << Fibonacci::F(n) << std::endl;
delete [] Fibonacci::memory;
return 0;
}