题目: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;
}

本文提供两种解决 HDU 2018 编程题的方法:一是通过模拟每年生育情况;二是利用递推公式进行计算。这两种方法均采用 C 语言实现。
376

被折叠的 条评论
为什么被折叠?



