斐波那契数列用c++编程:输入n,求第n个数怎么编?
1个回答
展开全部
void
main()
{
int
n;
int
nResult
=
0;
printf("计算斐波那契数列的第n个数的值。\n");
printf("请输入正整数n:");
scanf("%d",&n);
int
nPre=0;
int
nPrePre
=
0;
for(i=2;
i<=n;
i++)
//n<=2时,nResult是初始值0;
{
nResult
=
nPre+nPrePre;
nPrePre
=
nPre;
nPre
=
nResult;
}
printf("斐波那契数列中第n个数是:%d
\n",
nResult);
}
main()
{
int
n;
int
nResult
=
0;
printf("计算斐波那契数列的第n个数的值。\n");
printf("请输入正整数n:");
scanf("%d",&n);
int
nPre=0;
int
nPrePre
=
0;
for(i=2;
i<=n;
i++)
//n<=2时,nResult是初始值0;
{
nResult
=
nPre+nPrePre;
nPrePre
=
nPre;
nPre
=
nResult;
}
printf("斐波那契数列中第n个数是:%d
\n",
nResult);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询