题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2018
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
Input
输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。
Output
对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。
Sample Input
2
4
5
0
Sample Output
2
4
6
标签明确,当时想了好久,但知道了就很简单了,注意一下数值的范围就行了:
#include<stdio.h>
#include<math.h>
#include<string.h>
#define ll long long
#include<algorithm>
using namespace std;
ll shuzu[60];
int main()
{
int n,i;
shuzu[1]=1;
shuzu[2]=2;
shuzu[3]=3;
shuzu[4]=4;
for(i=5;i<60;++i)
shuzu[i]=shuzu[i-3]+shuzu[i-2]+shuzu[i-4];
while(~scanf("%d",&n))
{
if(n==0)
return 0;
else if(n<=4)
printf("%d\n",n);
else
printf("%d\n",shuzu[n]);
}
}