HDU-2018 母牛的故事
Problem Description
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
Input
输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。 n=0表示输入数据的结束,不做处理。
Output
对于每个测试实例,输出在第n年的时候母牛的数量。 每个输出占一行。
Sample Input
2
4
5
0
Sample Output
2
4
6
My first passage on CSDN.The reason why I use English is that my tpewritting is so lame.
But it might not be a bad thing for me to do this in English.
I attempts for times but all I got are WA! At first I use recursion(di gui) to solve the peoblem,but I gave up.
Finally I searched it on the internet and knew all it need is just the recurision(di tui??)
(p.s Why digui and ditui use the same word in Eng??)
This is my code.
#include<stdio.h>
int main()
{
int cow[56],n;
cow[0]=0;cow[1]=1;cow[2]=2;cow[3]=3;//先确定前几年的个数,可以在纸上自己画一下
for(int i=4;i<=55;i++){//递推
cow[i]=cow[i-1]+cow[i-3];
}
while(scanf("%d",&n)==1&&n){//按题目要求输入输出
printf("%d\n",cow[n]);
}
return 0;
}