Basic DP
Problem URL:https://vjudge.net/problem/HDU-2018
Describe:
There is a cow that gives birth to a heifer every year.Every heifer starts in the fourth year and also has a heifer at the beginning of each year.Please program how many cows are in the nth year?
Input:
The input data consists of multiple test instances, one for each test instance, including an integer n (0 < n < 55), the meaning of n as described in the title.n=0 means the end of the input data, no processing.
Sample Input:
2 4 5 0
Samle Output
2 4 6
DP[1] = 1 DP[2] = 2 DP[3] = 3 DP[4] = 4 DP[i] = DP[i-1] + DP[i-3] i>=5
AC code :
1 #include <iostream> 2 #include <algorithm> 3 #include <bits/stdc++.h> 4 using namespace std; 5 int DP[55]; 6 int main() 7 { 8 int n; 9 while(cin>>n && n!=0) 10 { 11 DP[1] = 1; 12 DP[2] = 2; 13 DP[3] = 3; 14 DP[4] = 4; 15 for(int j = 5;j < 55;j++) 16 DP[j] = DP[j-1] + DP[j-3]; 17 cout<<DP[n]<<endl; 18 } 19 return 0; 20 } 21