斐波那契数列:1,1,2,3,5,8,13,21,34……第20位数是( )程序怎么解?
2022-06-24
展开全部
C 语言实例 - 斐波那契数列
斐波那契数列指的是这样一个数列 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项开始,每一项都等于前两项之和。
实例 - 输出指定数量的斐波那契数列
#include <stdio.h>
int main()
{ int i, n, t1 = 0, t2 = 1, nextTerm;
printf("输出几项: ");
scanf("%d", &n);
printf("斐波那契数列: ");
for (i = 1; i <= n; ++i)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
} return 0;
}
运行结果:
输出几项: 10斐波那契数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
实例 - 输出指定数字前的斐波那契数列
#include <stdio.h>
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;
}
运行结果:
输入一个正数: 100斐波那契数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
斐波那契数列指的是这样一个数列 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项开始,每一项都等于前两项之和。
实例 - 输出指定数量的斐波那契数列
#include <stdio.h>
int main()
{ int i, n, t1 = 0, t2 = 1, nextTerm;
printf("输出几项: ");
scanf("%d", &n);
printf("斐波那契数列: ");
for (i = 1; i <= n; ++i)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
} return 0;
}
运行结果:
输出几项: 10斐波那契数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
实例 - 输出指定数字前的斐波那契数列
#include <stdio.h>
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;
}
运行结果:
输入一个正数: 100斐波那契数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
An=A(n-1)+A(n-2)
a20=a18+a19
a18=2584
a19=4181
a20=6765
a20=a18+a19
a18=2584
a19=4181
a20=6765
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询