C++ 递推法 斐波那契数列 兔子产仔

 1 #include "stdio.h"
 2 #include "iostream"
 3 
 4 int Fibonacci(int n)
 5 {
 6     int t1, t2;
 7     if (n == 1 || n == 2)
 8     {
 9         return 1;
10     }
11     else
12     {
13         t1 = Fibonacci(n-1);
14         t2 = Fibonacci(n-2);
15         return t1 + t2;
16     }
17 }
18 
19 int main()
20 {
21     int n, num;
22 
23     scanf("%d",&n);
24     num = Fibonacci(n);
25     printf("经过 %d 月的时间, 共能繁殖成 %d 对兔子!\n",n,num);
26     system("pause");
27     return 0;
28 }

注意思想:

第一个月:1对

第二个月:1对

第三个月:2对

第三个月:3对

第四个月:5对

。。。

从第三个月开始,每个月的兔子总对数等于前两个月兔子对数的总和。

posted @ 2019-07-07 11:20  我们都是大好青年  阅读(1461)  评论(0编辑  收藏  举报