hdu 2018 母牛的故事 动态规划入门题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2018

f[i][j] 表示第i天年龄为j的母牛个数,其中j=4代表所有年龄达到4岁的成年母牛,则:

  • f[1][4] = 1
  • f[1][i] = 0, i = 1,2,3
  • f[i][4] = f[i-1][4] + f[i-1][3]
  • f[i][3] = f[i-1][2]
  • f[i][2] = f[i-1][1]
  • f[i][1] = f[i][4]

代码:

#include <iostream>
#include <string>
using namespace std; const int maxn = 56; long long f[maxn][5]; void init() {
f[1][4] = 1;
for (int i = 2; i < maxn; i ++) {
f[i][4] = f[i-1][4] + f[i-1][3];
f[i][3] = f[i-1][2];
f[i][2] = f[i-1][1];
f[i][1] = f[i][4];
}
} void output(int i) {
cout << f[i][1] + f[i][2] + f[i][3] + f[i][4] << endl;
} int n; int main() {
init(); //for (int i = 1; i <= 10; i ++) output(i); while (cin >> n) {
if (!n) break;
output(n);
} return 0;
}
上一篇:wp8.1 Study13:在WP8.1中分享文件和数据


下一篇:HDU - 2018 - 母牛的故事(dp)