[C/C++]Fibonacci numbers

  

int fib(int n){
if (n ==1|| n ==2) {
return1;
}
return fib(n-1) + fib(n-2);
}

  

void fib_(int n, int* fibn, int* fibn_1){
if (n ==2|| n ==1) {
*fibn =1;
*fibn_1 =1;
}
else{
fib_(n
-1,fibn,fibn_1);
*fibn =*fibn +*fibn_1;
*fibn_1 =*fibn -*fibn_1;
}
};

int fib2(int n ){
int fibn;
int fib_1;
fib_(n,
&fibn,&fib_1);
return fibn;
}

 

 

int fib_tail_recur_(int n,int fib_n, int fib_n_minus1)
{
if(n == 3)
{
return fib_n + fib_n_minus1;
}
else
{
return fib_tail_recur_(n-1,fib_n + fib_n_minus1, fib_n);
}
}

int fib_tail_recur(int n)
{
if (n < 3){
return 1;
}else
{
return fib_tail_recur_(n,1,1);
}
}



posted @ 2011-08-06 21:11  嗷嗷  阅读(844)  评论(0编辑  收藏  举报