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;
}

猜你喜欢

转载自www.cnblogs.com/zifeiy/p/10722913.html