#include <stdio.h>
// 0 1 1 2 3 5 8
#define ERROR -1
int fibonacci(int n)
{
if(0 == n || 1 == n)
return n;
if(n < 0)
return ERROR;
int array[2] = {0, 1};
int curValue;
while(n-- != 1)
{
curValue = array[0] + array[1];
array[0] = array[1];
array[1] = curValue;
}
return curValue;
}
int main()
{
int size = 0;
while(scanf("%d", &size) != EOF)
{
printf("%d\n", fibonacci(size));
}
return 0;
}
斐波那契数列
最新推荐文章于 2023-10-31 11:28:12 发布