hdu 2018 母牛的故事(递推)

Problem Description

有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

Input

输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。

Output

对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。

Sample Input

2
4
5
0

Sample Output

2
4
6
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <cmath>
#include <map>

using namespace std;
typedef long long ll;

const double inf=1e20;
const int maxn=3000;//>2591
const int mod=1e9+7;

ll a[maxn];

int main(){
    a[0]=1;
    a[1]=2;
    a[2]=3;
    a[3]=4;
    for(int i=4;i<60;i++){
        a[i]=a[i-1]+a[i-3];
    }
    int n;
    while(scanf("%d",&n)!=EOF){
        if(n==0)break;
        printf("%lld\n",a[n-1]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wz-archer/p/12448182.html