예제 #1
0
        public static (int cumulativeRabbitsAge, int rabbitCount) MultiplyRabbitsIfAgeGreaterThanTwo(int totalYears)
        {
            int countId             = 0;
            int cumulativeRabbitAge = 0;

            rabbitsGreaterThanTwo = new List <Rabbit>();

            Rabbit rabbit = new Rabbit
            {
                RabbitId   = countId,
                RabbitName = "rabbit" + countId,
                Age        = 0
            };

            countId++;
            rabbitsGreaterThanTwo.Add(rabbit);

            // Code
            for (int year = 0; year < totalYears; year++)
            {
                foreach (Rabbit item in rabbitsGreaterThanTwo.ToArray())
                {
                    if (item.Age >= 3)
                    {
                        Rabbit newRabbit = new Rabbit
                        {
                            RabbitId   = countId,
                            RabbitName = "rabbit" + countId,
                            Age        = 0
                        };
                        countId++;
                        rabbitsGreaterThanTwo.Add(newRabbit);
                    }
                    item.Age++;
                }
            }

            rabbitsGreaterThanTwo.ForEach(r => cumulativeRabbitAge += r.Age);
            return(cumulativeRabbitAge, rabbitsGreaterThanTwo.Count);
        }
예제 #2
0
파일: Program.cs 프로젝트: hematshah/Labs
        public static (int cumulativeRabbitAge, int rabbitCount) MultiplyRabbitsAfterAgeThreeReached(int totalYears)
        {
            int cumulativeRabbitAge = 0;

            rabbits = new List <Rabbit>();

            var rabbit0 = new Rabbit
            {
                RabbitId   = 0,
                RabbitName = "Rabbit0",
                Age        = 0
            };

            rabbits.Add(rabbit0);


            for (int years = 0; years < totalYears; years++)
            {
                foreach (var rabbit in rabbits.ToArray())
                {
                    if (rabbit.Age >= 3)
                    {
                        var newRabbit = new Rabbit();
                        rabbits.Add(newRabbit);
                        rabbit.Age++;
                    }
                    else
                    {
                        rabbit.Age++;
                    }
                }
            }

            rabbits.ForEach(r => cumulativeRabbitAge += r.Age);
            return(cumulativeRabbitAge, rabbits.Count);
        }