## HDU 2018 Cow Story DP

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
DP = 2
DP = 3
DP = 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;
6 int main()
7 {
8     int  n;
9     while(cin>>n && n!=0)
10     {
11         DP = 1;
12         DP = 2;
13         DP = 3;
14         DP = 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    

posted on 2019-07-14 12:02  Algorithm_Ethusiast  阅读(115)  评论(0编辑  收藏

• 随笔 - 10
• 文章 - 0
• 评论 - 0
• 引用 - 0