Posted by felipe tonello on Fri 30 Jan 05:00
report abuse | download | new post
- #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;
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.