ClassCow
{
Public IntAge = 1;// Initialize the age to 1
}
/// <Summary>
/// A farm has a cow. A cow is only one year old and has a calf only when it is four years old. After four years old, a calf is generated every year.
/// Assume that every time a cow is born, and that the cow has been born and has been born for 4 years, there is no death. How many cows are there after N years?
/// </Summary>
Static VoidMain (String[] ARGs)
{
List <cow> cows =NewList <cow> ();
Cow cow =NewCow ();
Cows. Add (COW );
IntN = 10;// 10 years
For(IntI = 1; I <= N; I ++)
{
// If cows. Count is directly used as the cycle end condition, the following generic set loops will change dynamically, resulting in more loops.
IntTemp = cows. count;
For(IntJ = 0; j <temp; j ++)
{
Cows [J]. Age ++;// One year after the cycle, age + +
If(COWS [J]. age> = 4)// Traverse all the cows in the set. If the age is greater than or equal to 4 years old, a calf is generated.
{
Cow =NewCow ();
Cows. Add (COW );
}
}
}
Console. writeline ("{0} years later, there were {1} cows on the farm", N, cows. Count );
Console. Readline ();
}