HDU2018-母牛的故事

描述:

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

代码:

  第n年的牛,等于第n-1年的牛(已有的)+第n-3年的牛(第n年可生产的)。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;

int F(int n){
    if( n==1 ) return 1;
    if( n==2 ) return 2;
    if( n==3 ) return 3;
    return F(n-1)+F(n-3);
}

int main(){
    int n;

    while( scanf("%d",&n)!=EOF ){
        if( n==0 ) break;
        printf("%d
",F(n));
    }
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/lucio_yz/p/4701655.html