hdu2018母牛的故事
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
Input
输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。 n=0表示输入数据的结束,不做处理。Output对于每个测试实例,输出在第n年的时候母牛的数量。 每个输出占一行。
Sample Input2 4 5 0
Sample Output2 4 6
斐波拉契
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e6+5; 4 const int INF=1e9+7; 5 int n,f[60]; 6 template <class t>void red(t &x) 7 { 8 x=0; 9 int w=1; 10 char ch=getchar(); 11 while(ch<'0'||ch>'9') 12 { 13 if(ch=='-') 14 w=-1; 15 ch=getchar(); 16 } 17 while(ch>='0'&&ch<='9') 18 { 19 x=(x<<3)+(x<<1)+ch-'0'; 20 ch=getchar(); 21 } 22 x*=w; 23 } 24 void input() 25 { 26 freopen("input.txt","r",stdin); 27 } 28 int main() 29 { 30 //input(); 31 f[1]=1; 32 f[2]=2; 33 f[3]=3; 34 f[4]=4; 35 for(int i=5;i<=60;++i) 36 f[i]=f[i-1]+f[i-3]; 37 while(scanf("%d",&n)==1&&n) 38 printf("%d\n",f[n]); 39 return 0; 40 }