/************************************************************************
*
* 输入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));
}
}
*
* 输入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));
}
}