• 再谈奶牛问题


    在博客园上看到很多人讨论的”在csdn上看到奶牛问题,写了下算法“和“奶牛问题”,觉得好玩,自己尝试着用面向对象的方法算了一下,练练手,没有技术含量,记录下来。
    一、问题还原
    一只刚出生的小牛,4年后生一只小牛,以后每年生一只。现有一只刚出生的小牛,问20年后共有牛多少只?

    ps:看到很多人在原题处留言说这题出的不严谨云云。下面的所有实现都是在理想状态下,特此声明。
    二、具体实现
    1、奶牛实体类

    代码
      public class Cow
        {

            
    public Cow()
            {

            }

            
    public Cow(int age, int generation, int id, int parentId)
            {
                
    this.Age = age;
                
    this.Generation = generation;
                
    this.Id = id;
                
    this.ParentId = parentId;
            }

            
    /// <summary>
            
    /// 岁数
            
    /// </summary>
            public int Age { getset; }

            
    /// <summary>
            
    /// 第几代
            
    /// </summary>
            public int Generation { getset; }

            
    /// <summary>
            
    /// 唯一标识 id
            
    /// </summary>
            public int Id { getset; }

            
    /// <summary>
            
    /// 母亲id
            
    /// </summary>
            public int ParentId { getset; }
        }

     2、生奶牛方法(非递归)

    代码
            static List<Cow> listCows = new List<Cow>();
            
    public static void GetBirth(int year)
            {
                List
    <Cow> listBornCows = new List<Cow>();//用于添加新生的奶牛
                Cow firstCow = new Cow(1110);
                listCows.Add(firstCow);
                
    for (int i = 0; i < year; i++)
                {
                    
    foreach (Cow item in listCows)
                    {
                        item.Age
    ++//年龄自增
                        if (item.Age > 4)
                        {
                            
    //生出一头牛
                            Cow birth = new Cow(1, item.Generation + 1, listCows.Count + 1, item.Id);
                            listBornCows.Add(birth); 
    //添加新生的奶牛
                        }
                    }
                    listCows.AddRange(listBornCows);
                    listBornCows.Clear();
                }
            }

     3、输出
    下面的显示是按照母亲id来显示结果的,您也可以改成按照第几代或者岁数显示。

    代码
            public static List<Cow> GetCowByParentId(int parentId)
            {
                List
    <Cow> result = new List<Cow>();
                
    foreach (Cow item in listCows)
                {
                    
    if (item.ParentId == parentId)
                    {
                        result.Add(item);
                    }
                }
                
    return result;
            }

            
    public static void ShowCows()
            {
                
    int count = 0;
                
    if (listCows != null)
                {
                    count 
    = listCows.Count;
                }
                Console.WriteLine(
    string.Format("After 20 years,cows count:{0}", count.ToString()));

                
    /*下面按照所属母亲(ParentId)显示对应奶牛数*/
                
    int maxParentId = 0;
                
    if (listCows.Count > 0)
                {
                    
    //按照所属母亲 逆序排序
                    listCows.Sort(delegate(Cow left, Cow right) { return right.ParentId.CompareTo(left.ParentId); });
                    maxParentId 
    = listCows[0].ParentId;
                }
                
    for (int i = 0; i < maxParentId; i++)
                {
                    List
    <Cow> listModels = GetCowByParentId(i);
                    Console.WriteLine(
    string.Format("Cow_{0}'s children as follows:", i));
                    
    if (listModels.Count == 0)
                    {
                        Console.WriteLine(
    "Has no any child!");
                    }
                    
    else
                    {
                        
    foreach (Cow item in listModels)
                        {
                            Console.WriteLine(
    string.Format("Age:{0},Id:{1},Generation:{2}", item.Age, item.Id, item.Generation));
                        }
                    }
                }
            }

     最后贴一下完整代码:

    代码
       class Program
        {
            
    static List<Cow> listCows = new List<Cow>();
            
    static int maxYear = 20;
            
    public static void GetBirth(int year)
            {
                List
    <Cow> listBornCows = new List<Cow>();//用于添加新生的奶牛
                Cow firstCow = new Cow(1110);
                listCows.Add(firstCow);
                
    for (int i = 0; i < year; i++)
                {
                    
    foreach (Cow item in listCows)
                    {
                        item.Age
    ++//年龄自增
                        if (item.Age > 4)
                        {
                            
    //生出一头牛
                            Cow birth = new Cow(1, item.Generation + 1, listCows.Count + 1, item.Id);
                            listBornCows.Add(birth); 
    //添加新生的奶牛
                        }
                    }
                    listCows.AddRange(listBornCows);
                    listBornCows.Clear();
                }
            }

            
    public static List<Cow> GetCowByParentId(int parentId)
            {
                List
    <Cow> result = new List<Cow>();
                
    foreach (Cow item in listCows)
                {
                    
    if (item.ParentId == parentId)
                    {
                        result.Add(item);
                    }
                }
                
    return result;
            }

            
    public static void ShowCows()
            {
                
    int count = 0;
                
    if (listCows != null)
                {
                    count 
    = listCows.Count;
                }
                Console.WriteLine(
    string.Format("After 20 years,cows count:{0}", count.ToString()));

                
    /*下面按照所属母亲(ParentId)显示对应奶牛数*/
                
    int maxParentId = 0;
                
    if (listCows.Count > 0)
                {
                    
    //按照所属母亲 逆序排序
                    listCows.Sort(delegate(Cow left, Cow right) { return right.ParentId.CompareTo(left.ParentId); });
                    maxParentId 
    = listCows[0].ParentId;
                }
                
    for (int i = 0; i < maxParentId; i++)
                {
                    List
    <Cow> listModels = GetCowByParentId(i);
                    Console.WriteLine(
    string.Format("Cow_{0}'s children as follows:", i));
                    
    if (listModels.Count == 0)
                    {
                        Console.WriteLine(
    "Has no any child!");
                    }
                    
    else
                    {
                        
    foreach (Cow item in listModels)
                        {
                            Console.WriteLine(
    string.Format("Age:{0},Id:{1},Generation:{2}", item.Age, item.Id, item.Generation));
                        }
                    }
                }
            }

            
    static void Main(string[] args)
            {
                GetBirth(maxYear);
                ShowCows();
                Console.ReadLine();
            }
        }



     


    作者:Jeff Wong
    出处:http://jeffwongishandsome.cnblogs.com/
    本文版权归作者和博客园共有,欢迎围观转载。转载时请您务必在文章明显位置给出原文链接,谢谢您的合作。

  • 相关阅读:
    POJ 1611 The Suspects
    POJ 2001 Shortest Prefixes(字典树)
    HDU 1251 统计难题(字典树 裸题 链表做法)
    G++ C++之区别
    PAT 乙级 1013. 数素数 (20)
    PAT 乙级 1012. 数字分类 (20)
    PAT 乙级 1009. 说反话 (20)
    PAT 乙级 1008. 数组元素循环右移问题 (20)
    HDU 6063 17多校3 RXD and math(暴力打表题)
    HDU 6066 17多校3 RXD's date(超水题)
  • 原文地址:https://www.cnblogs.com/jeffwongishandsome/p/1620338.html
Copyright © 2020-2023  润新知