兔子生兔子问题

来源:互联网 发布:java中变量的命名规则 编辑:程序博客网 时间:2024/04/24 00:42

命题:

有一只小兔子,成长期1个月,怀孕期1个月,问1年之后一共有多少个兔子?


#include <stdio.h>int main(int argc, char *argv[]){        int count = 1;        int x = 1;        int c = 0;        int h = 0;        int i;        int temp_x, temp_c, temp_h;        printf("   %10s %10s %10s\n", "new", "grow", "pregnant");        for(i = 1; i <= 12; i++)        {                count = count + h;                temp_c = c;                temp_x = x;                printf("%2d:%10d,%10d,%10d\n", i, x, c, h);                x = c + h;                c = temp_x;                h = temp_c + h;        }        printf("Total count = %d\n", count);        return 0;}


          new       grow   pregnant
 1:         1,         0,         0
 2:         0,         1,         0
 3:         1,         0,         1
 4:         1,         1,         1
 5:         2,         1,         2
 6:         3,         2,         3
 7:         5,         3,         5
 8:         8,         5,         8
 9:        13,         8,        13
10:        21,        13,        21
11:        34,        21,        34
12:        55,        34,        55
Total count = 144




原创粉丝点击