斐波那契数列:指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368........
这个数列从第3项开始,每一项都等于前两项之和。
代码:
int main()
{
int t1 = 0, t2 = 1, nextTerm = 0, n;
printf("输入一个正数: ");
scanf("%d", &n);
// 显示前两项
printf("斐波那契数列: %d, %d, ", t1, t2);
nextTerm = t1 + t2;
while (nextTerm <= n)
{
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
运行效果: