苦思冥想一个C语言问题 我才学到指针和结构变量 给大家做看看这个问题,我想不出来,顺便学习一下
一对兔子 长到3个月成熟,3个月后每个月生一对兔子。生下的小兔子也符合这个条件。问:输入月份求出该月份的兔子总数。

没病的人说有病的人有病,有病的人说没病的人有病。到底是谁有病?
#include<stdio.h> void getmoon(int n)/*n为共需输出几个月的数*/ { int i=0,s,sum,x=1,y1=0,y2=0,y3=0;/*x用来存成年的,y用来存小的,i用来存月数*/ for(i=1;i<=n;i++) { s=y3; /*3月龄的现再也应成年了,下面行同理*/ y3=y2; y2=y1; y1=x*2; /*这个月新生小免数*/ x+=s; /*这月底的成年数,下月要生小免的*/ sum=y3+y2+y1+x; printf("i moonth: %d\n",sum); } } main() { int sum; printf("Please enter a number:"); scanf("%d",&sum); getmoon(sum); }
#include <stdio.h> #include <stdlib.h> struct Month_rabbit { long int old_rabbit, //成熟的兔子 new_rabbit, //新生的兔子 unew_rabbit, //非新生未成熟的兔子 sum_rabbit; //该月为止兔子总数 } ; void rabbit_sum(int month, struct Month_rabbit *prabbit) { if(month<1) { printf("输入有误!"); exit(1); } for(int i=1;i <= month; i++ ) { switch(i) { case 1 : prabbit->new_rabbit= 2; prabbit->old_rabbit = 0; prabbit->unew_rabbit = 0; prabbit->sum_rabbit = 2; break; case 2 : prabbit->new_rabbit= 0; prabbit->old_rabbit = 0; prabbit->unew_rabbit = 2; prabbit->sum_rabbit = 2; break; case 3 : prabbit->new_rabbit= 0; prabbit->old_rabbit = 0; prabbit->unew_rabbit = 2; prabbit->sum_rabbit = 2; break; default : prabbit->old_rabbit = (prabbit-3)->new_rabbit + (prabbit-1)->old_rabbit ;//本月成熟兔子等于往前数三个月月份新生兔子与本月前一个月月份成熟兔子之和 prabbit->unew_rabbit = (prabbit-1)->unew_rabbit + (prabbit-1)->new_rabbit - (prabbit-3)->new_rabbit ;//本月非新生未成熟兔子等于前一个月份非新生未成熟兔子加前一个月份新生兔子的和减去本月往前数三个月月份的新生兔子数 prabbit->new_rabbit = prabbit->old_rabbit ;//本月新生兔子数等于本月成熟的兔子数 prabbit->sum_rabbit = prabbit->old_rabbit + prabbit->new_rabbit + prabbit->unew_rabbit ;//本月为止兔子总数为本月成熟的兔子数和新生的兔子数以及非新生未成熟兔子数之和 } prabbit++; } } int main(void) { int month; long int total = 0; struct Month_rabbit rabbit[20]; printf("请输入月份:"); scanf("%d", &month); rabbit_sum(month, rabbit); printf("到第%d月为止共有兔子:%ld\n", month,rabbit[month-1].sum_rabbit); return 0; }