题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2018点击打开链接
母牛的故事
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 85407 Accepted Submission(s): 42508
Problem Description
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
Input
输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。
n=0表示输入数据的结束,不做处理。
Output
对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。
每个输出占一行。
Sample Input
2 4 5 0
Sample Output
2 4 6
多分析可以发现 dp[i]=dp[i-1]+dp[i-3]
这一年的母牛就是上一年的牛再加上四年前那些牛生下来的小牛
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <limits>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
long long int a[60];
a[1]=1;
a[2]=2;
a[3]=3;
a[4]=4;
for(int i=5;i<55;i++)
a[i]=a[i-1]+a[i-3];
int mid;
while(~scanf("%d",&mid)&&mid)
printf("%lld\n",a[mid]);
}