//生后隔年成熟,成熟后次年生崽
#include <stdio.h>
#include <malloc.h>
#include <time.h>
int main()
{
int *ptuzi = NULL,i = 0, nian = 0;
time_t tstart = 0,tfinish = 0,tuse = 0;
scanf("%d",&nian);
fflush(stdin);
ptuzi = (int*)calloc(nian + 1,sizeof(int));
ptuzi[3] = ptuzi[2] = ptuzi[1] = ptuzi[0] = 1;
tstart = clock();
for (i=4;i<nian+1;i++)
{
ptuzi[i] = ptuzi[i-1] + ptuzi[i-3];
printf("第%d年的兔子总数为%d\n",i,ptuzi[i]);
}
tfinish = clock();
printf("用时%lf秒\n",((double)tfinish - tstart)/CLOCKS_PER_SEC);
free(ptuzi);
printf("递归算法\n");
for (i=4;i<nian+1;i++)
{
tstart = clock();
printf("第%d年的兔子总数为%d\t",i,XiaoTutu(i)); //没声明在先,却可以调用????
tfinish = clock();
printf("用时%5.2lf秒\n",((double)tfinish - tstart)/CLOCKS_PER_SEC);
}
return 0;
}
int XiaoTutu(int yea)
{
if (yea < 4)
{
return 1;
}
else
{
return XiaoTutu(yea-1) + XiaoTutu(yea-3);
}
}
递归级数高的时候太浪费时间了,偶2G内存,主频2.6还这么慢,真怕嘎蹦把我的栈搞爆了,有图有真相