母牛的故事

母牛的故事

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 79544    Accepted Submission(s): 39497


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

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

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

Sample Input
2 4 5 0
 

Sample Output
2 4 6

第n年的牛,是前一年牛的头数,加上生下的头数,也就是能生产的母牛头数从第四年开始(n-3)


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

int f(int n)
{
    if(n<=3 )   return n;
    else return f(n-1) + f(n-3);
}

int main()
{
    int m;
    while (cin>>m)
    {
        if (m == 0) break;
        cout<<f(m)<<endl;
    }
    return 0;
}




posted @ 2017-02-27 12:09  link98  阅读(132)  评论(0)    收藏  举报