羊生羊问题
//一只母羊一年可以生一头小母羊,一头小母羊4年可成为一只大母羊,求n年之后羊的数量
#include
using namespace std;
#define MAX 20
int a[MAX] = { 0 };
int tempt1 = 1, sum = 0, tempt2=1, n;
void bfs(int s)
{
if (s > n)
{
cout << tempt2 << endl;
return;
}
tempt1 = tempt1 + a[4];//今年新生的小羊
tempt2 = tempt2 + tempt1 ;
for (int i = 4; i>=2; i--)//羊的年龄曾1
{
a[i] = a[i - 1];
}
a[1] = tempt1;//今年新生的羊为1岁
bfs(s + 1);
}
int main()
{
cin >> n;
bfs(1);
return 0;
}
如有不完善之处,请指教。