/************************************************************************
*
*  输入n,用最快的方法求斐波那契数列的第n项 
*
/***********************************************************************
*/

#include 
"stdio.h"

int fabonacci(int n)
{
    
int first = 0, second = 1;
    
int tmp = 0;
    
if (n == 1)
    {
        
return 0;
    }
    
if (n == 2)
    {
        
return 1;
    }
    
while(n-- > 2)
    {
        tmp 
= first + second;
        first 
= second;
        second 
= tmp;
    }
    
return second;
}

void main()
{
    
for (int i = 1; i <= 20; i++)
    {
        printf(
"fabonacci(%d) = %d\n", i, fabonacci(i));
    }
}

相关文章:

  • 2021-12-02
  • 2022-12-23
  • 2022-02-22
  • 2022-12-23
  • 2021-06-22
  • 2021-04-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-06-15
相关资源
相似解决方案