c++:题目是:假设有一头小母牛,从出生起第四年头开始每年生一头母牛,按此规律:

来源:百度知道 编辑:UC知道 时间:2024/05/17 02:06:20
1.第n年时有多少头母牛?
2.若一头牛的寿命名10年,又该如何计算?
生了后亡

不好意思,第一次没看清出题,现在改过来了,其实不是想象那么复杂,这个题要从“生活”的角度来想,就很简单了。
1、牛的数量为去年的数量加上四年前的数量,因为只要是四年前的牛在第四年头都能马上生小牛。
2、牛的数量为去年的数量加上四年前的数量-11年前的数量,之所以减11年前的数量,是因为牛是先生后死,10年前马上要死的牛,还能在10年后最后生一次。
代码如下:
#include<iostream>
using namespace std;
int main()
{
int Cows(int year,int BirthCycle);
int CowsWithDeath(int year,int BirthCycle,int DeathCycle);
cout<<"输入年数"<<endl;
int year;
cin>>year;
cout<<"小母牛不死,"<<year<<"年后共"<<Cows(year-1,3)<<"头牛"<<endl;
cout<<"小母牛能活10年,"<<year<<"年后共"<<CowsWithDeath(year-1,3,9)<<"头牛"<<endl;
return 0;
}
int Cows(int year,int BirthCycle)
{
if(year<BirthCycle)
return 1;
return Cows(year-1,BirthCycle)+Cows(year-BirthCycle,BirthCycle);
}
int CowsWithDeath(int year,int BirthCycle,int DeathCycle)