Advertisement
cardel

Recursión Fibonnacci

Nov 29th, 2016
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. /*
  2.   Asignatura: PROGRAMACIÓN ORIENTADA A OBJETOS (IPOO) 750081M
  3.   Archivo: iterativo.cpp
  4.   Fecha creación: 28 - Noviembre - 2016
  5.   Fecha última modificación: 28 - Noviembre - 2016
  6.   Autor: Carlos Andres Delgado
  7. */
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. int fibonnaci(int n){
  12.     if (n == 0) return 0;
  13.     if (n == 1) return 1;
  14.     return fibonnaci(n-1)+fibonnaci(n-2);
  15.    
  16. }
  17.  
  18. int fibonnaciEstado(int n, int i, int fn1, int fn2)
  19. {
  20.     if(n == 0) return 0;
  21.     if(n == 1) return 1;
  22.     if(i == n) return fn1+fn2;
  23.    
  24.     return fibonnaciEstado(n,(i+1),fn1+fn2, fn1);
  25.    
  26. }
  27. int main(){
  28.     int numero;
  29.     cout << "ingrese un núimero" << endl;
  30.     cin >> numero;
  31.    
  32.     cout <<"El resultado es: "<< fibonnaci(numero) << endl;
  33.    
  34.     int f = 0;
  35.     int fn1 = 1;
  36.     int fn2 = 0;
  37.     if (numero == 0) f = 0;
  38.     if (numero == 1) f = 1;
  39.     for(int i = 2; i<=numero; i++){
  40.             f = fn1 + fn2;
  41.             fn2 = fn1;
  42.             fn1 = f;
  43.     }
  44.    
  45.     cout <<"El resultado es: "<< f << endl;
  46.     cout << "El estado es: "<< fibonnaciEstado(numero,2,1,0);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement