Count the number of cows on the farm after N years

Source: Internet
Author: User
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 ();

 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.