用递归求菲波拉契序列第N项的值

 1 #include <stdio.h>
 2 /*
 3 题目:用递归求菲波拉契序列第N项的值
 4 */
 5 int func(int n);
 6 
 7 int main(void)
 8 {
 9     int N;
10 gogogo:    printf("输入要求的项数(例:求第3项的值输入3)\n");
11         scanf("%d",&N);
12         printf("第%d项的值 = %d\n",N,func(N));
13         
14         goto gogogo;
15         
16         return 0;
17 }
18 int func(int n)
19 {
20     if(1 == n||2 == n)//结束条件
21         return 1;
22     else
23         return func(n-1)+func(n-2);//通项公式:Fn = Fn-1+Fn-2;
24 }

 

posted on 2018-08-28 16:38  王朝马汉  阅读(243)  评论(0编辑  收藏  举报

导航