求C++高手解答

来源:百度知道 编辑:UC知道 时间:2024/08/30 11:07:14
一个奶牛厂有一只奶牛,它从第1年开始每年生一只小奶牛,到了第4年小奶牛也开始生奶牛。就这样一直生,求N年时奶牛的数量???
我编的程序虽能求出正确结果,但较复杂,求c++高手的编程方法。
奶牛数量:
第1年 2 第2年 3 第3年 4
第4年 6 第5年 9 第6年 13
第7年 19 第8年 27 第9年 37
……

函数如下:
//求第N年的奶牛数
int Fun(int N)
{
if(N<0) return 1;
else if(N<4) return N+1;
else return 2*Fun(N-3)+Fun(N-4)+Fun(N-5);
}
int main()
{
//在这里调用Fun函数就可以了
}

//另外你的程序有错误,你自己手动演算一下第8年的就知道了,从第8年开始你的结果就出错了.

N年是几年?