• 奶牛问题,别人写的,自己试了一下.


      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 
      6 namespace CowCount
      7 {
      8     public class Cow
      9     {
     10         public int Age { getset; }
     11         public int Generation { getset; }
     12         public int Id { getset; }
     13         public int ParentId { getset; }
     14 
     15         public Cow() { }
     16 
     17         public Cow(int age, int generation, int id, int parentId)
     18         {
     19             this.Age = age;
     20             this.Generation = generation;
     21             this.Id = id;
     22             this.ParentId = parentId;
     23         }
     24     }
     25     class Program
     26     {
     27         static List<Cow> listCows = new List<Cow>();
     28         static int maxYear = 20;
     29         public static void GetBirth(int year)
     30         {
     31             List<Cow> listBornCows = new List<Cow>(); //添加新生的奶牛
     32             Cow firstCow = new Cow(1110);
     33             listCows.Add(firstCow);
     34             for (int i = 0; i < year; i++)
     35             {
     36                 foreach (Cow item in listCows)
     37                 {
     38                     item.Age++//年龄自增
     39                     if (item.Age > 4)
     40                     {
     41                         Cow birth = new Cow(1,item.Generation + 1, listCows.Count + 1, item.Id);
     42                         listBornCows.Add(birth); //添加新生的奶牛
     43                     }
     44                 }
     45                 listCows.AddRange(listBornCows);
     46                 listBornCows.Clear();
     47             }
     48         }
     49 
     50         public static List<Cow> GetCowByParentId(int parentId)
     51         {
     52             List<Cow> result = new List<Cow>();
     53             foreach (Cow item in listCows)
     54             {
     55                 if (item.ParentId == parentId)
     56                     result.Add(item);
     57             }
     58             return result;
     59         }
     60 
     61         public static void ShowCows()
     62         {
     63             int count = 0;
     64             if (listCows != null)
     65             {
     66                 count = listCows.Count;
     67             }
     68             Console.WriteLine(string.Format("After 20 years,cows count:{0}", count.ToString()));
     69 
     70             //按照所属于母亲显示对应奶牛数
     71             int maxParentId = 0;
     72             if (listCows.Count > 0)
     73             {
     74                 listCows.Sort(delegate(Cow left, Cow right) { return right.ParentId.CompareTo(left.ParentId); });
     75                 maxParentId = listCows[0].ParentId;
     76             }
     77             for (int i = 0; i < maxParentId; i++)
     78             {
     79                 List<Cow> listModels = GetCowByParentId(i);
     80                 Console.WriteLine(string.Format("Cow_{0}'s children as follows:",i));
     81                 if(listModels.Count == 0)
     82                 {
     83                     Console.WriteLine("Has no any child!");
     84 
     85                 }
     86                 else
     87                 {
     88                     foreach (Cow item in listModels)
     89                     {
     90                         Console.WriteLine(string.Format("Age:{0},Id:{1},Generation:{2}", item.Age, item.Id, item.Generation));
     91                     }
     92                 }
     93             }
     94         }
     95         static void Main(string[] args)
     96         {
     97             GetBirth(maxYear);
     98             Console.WriteLine(listCows.Count);
     99             //ShowCows();
    100             Console.ReadLine();
    101         }
    102     }
    103 }
    104 


  • 相关阅读:
    ASP.NET Web API是如何根据请求选择Action的?[上篇]
    Ruby的对象模型
    MongoDB学习3
    Linux目录树详细说明
    Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)
    [置顶] SQL注入安全分析
    3.9 聚集和联接
    Qt之QTemporaryFile(文件名唯一,且可以自动删除)
    调用Windows属性窗口(居然是通过注册表来调用的)
    QTextEdit中选中文本修改字体与颜色,全部文本修改字体与颜色(设置调色板的前景色、背景色、文字颜色以及基色)
  • 原文地址:https://www.cnblogs.com/chenqingwei/p/1620480.html
Copyright © 2020-2023  润新知