一个农户有一头牛,三年后开始生小牛,小牛3年后又开始生, 问N年后多少头牛, 假设生的都是母牛
struct Cow
{
int m_iYear;
Cow()
{
m_iYear = 0;
}
void UpdateYear()
{
m_iYear++;
}
bool ShouldBorn()
{
return m_iYear > 3;
}
};
void GetCow(int n)
{
std::vector<Cow> vecCows;
Cow stCow;
//第一头牛
vecCows.push_back(stCow);
for( int i = 1; i <= n; i++ )
{
int iCowCount = vecCows.size();
for(int j = 0; j < iCowCount; j++)
{
vecCows[j].UpdateYear();
if(vecCows[j].ShouldBorn())
{
vecCows.push_back(Cow());
}
}
cout << i << " years later, there are [" << vecCows.size() << "] cow" << endl;
}
}