方法一:迭代
#include <stdio.h>
typedef struct
{
int a;//小老鼠
int b;//大老鼠
int c;//濒死的老鼠
}MOUSE;
int mouse(int);
int main()
{
int n;
printf("please input the numbern");
scanf("%d",&n);
printf("mouse number is :%dn",mouse(n));
return 0;
}
int mouse(int week)
{
MOUSE mos;
int i,tmp;
mos.a=1;
mos.b=0;
mos.c=0;
for (i=0;i<week;++i)
{
tmp=mos.a;
mos.a=mos.b+mos.c;
mos.c=mos.b;
mos.b=tmp;
printf("小老鼠:%d;大老鼠:%d;濒死老鼠:%d;总老鼠:%dn",mos.a,mos.b,mos.c,mos.a+mos.b+mos.c);
}
return (mos.a+mos.b+mos.c);
}
方法二:递归
#include <stdio.h>
typedef struct
{
int a;//小老鼠
int b;//大老鼠
int c;//濒死的老鼠
}MOUSE;
void mouse(int week,MOUSE *mos);
int main()
{
int week;
MOUSE mos;
printf("Please input the number of week:");
scanf("%d",&week);
mouse(week,&mos);
return 0;
}
void mouse(int week,MOUSE *mos)
{
int tmp;
if (week == 0)
{
mos->a = 1;
mos->c = 0;
mos->b = 0;
return;
}
else
{
mouse(--week,mos);
tmp = mos->a;
mos->a = mos->b+mos->c;
mos->c = mos->b;
mos->b = tmp;
printf("小老鼠:%d;大老鼠:%d;濒死老鼠:%d;总老鼠:%dn",mos->a,mos->b,mos->c,mos->a+mos->b+mos->c);
}
}
方法三:迭代
#include <stdio.h>
int f(int n)//新生小老鼠
{
if(n == 0)return 1;
if(n < 0)return 0;
return f(n-2)+f(n-3);
}
int F(int n)
{
return f(n)+f(n-1)+f(n-2);
}
int main()
{
int i;
scanf("%d",&i);
printf("%dn",F(i));
}
周数: 老 大 小
0 0 0 1
1 0 1 0
2 1 0 1
3 0 1 1
4 1 1 1
5 1 1 2
6 1 2 2
7 2 2 3
8 2 3 4
以第八周为例,小老鼠4=第六周的2只小老鼠变成大老鼠生下的,和第5周两只小老鼠变成的老老鼠生下的。
相关知识
老鼠怎么繁殖 老鼠怎么繁殖啊
老鼠怎么繁殖 老鼠是怎么繁殖的
老鼠如何繁殖
老鼠怎么繁殖?
老鼠的繁殖
老鼠繁殖需要公老鼠吗
老鼠是怎么繁殖的 老鼠繁殖方法
老鼠繁殖多长时间一窝
老鼠的繁殖能力
老鼠怎么繁殖
网址: 老鼠繁殖 https://m.mcbbbk.com/newsview1050330.html
上一篇: 玄凤繁殖计划尚未开始,准备放弃的 |
下一篇: 哪个宠物鼠最好养活一些? |