题目:acm.hdu.edu.cn/showproblem.php?pid=2018
解一:模拟每年生育情况
#include <stdio.h>
int main()
{
int n,i,mature,one,two,three;
while(~scanf("%d",&n) && n)
{
for(mature=i=1,one=two=three=0;i<n;i++)
{
mature += three;
three = two;
two = one;
one = mature;
}
printf("%d\n",mature+ one+ two+ three);
}
return 0;
}
解二:发现递推式
#include <stdio.h>
#include <string.h>
int ans[56];
int a(int n)
{
if(ans[n])
return ans[n];
else
return ans[n]=a(n-1)+a(n-3);
}
int main()
{
int n;
memset(ans,0,sizeof(ans));
ans[1] = 1,ans[2] = 2,ans[3] = 3;
while(~scanf("%d",&n) && n)
printf("%d\n",a(n));
return 0;
}