//递归求第n个斐波那契数
#include <stdio.h>
#include <windows.h>
#pragma warning(disable:4996)
int main()
{
int a = 0;
printf(“请输入一个数:”);
scanf("%d", &a);
int t = fib(a);
printf(“第n个斐波那契数的值为:%d”, t);
system(“pause”);
}
int fib(a)
{
if (a<3)
{
return 1;
}
return fib(a - 1) + fib(a - 2);
}
//非递归求第n个斐波那契数
#include <stdio.h>
#include <windows.h>
#pragma warning(disable:4996)
int main()
{
int a = 0;
printf(“请输入一个数:”);
scanf("%d", &a);
int t = fib(a);
printf(“第n个斐波那契数的值为:%d”, t);
system(“pause”);
}
int fib(a)
{
int b = 1;
int z = 1;
int x = 1;
while (a > 2)
{
x = b + z;
b = z;
z = x;
a–;
}
return x;
}
C语言求斐波那契数(递归和非递归)
最新推荐文章于 2022-04-25 00:14:34 发布