递推的一个水题。一开始规律找错了,果然太水了,规律是f(n)=f(n-1)+f(n-3)
Problem Description
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第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>
int main()
{
__int64 one=0,two=0,three=0,four=1,temp=0,crow[55]={0};
crow[1]=1;
for(int n=2;n<55;n++)
{
four+=three;
temp=four;
three=two;
two=one;
one=temp;
crow[n]=one+two+three+four;
}
int x;
while(scanf("%d",&x)!=EOF&&x!=0)
{
printf("%I64d\n",crow[x]);
}
return 1;
}