递归计算斐波那契数(递归不适合,只是上课记录)

时间:2022-10-26 19:57:33

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>

int count = 0;

int Fid(int n)

{

if (n == 3)//看第3个斐波那契数被计算了几次

{

 count++;

}

if (n <= 2)

 return 1;

else

 return Fid(n - 1) + Fid(n - 2);


}

int main()

{

int n = 0;

scanf("%d", &n);

int ret = 0;

 ret = Fid(n);

printf("ret=%d\n", ret);

printf("count=%d\n", count);



return 0;

}