再谈奶牛问题
在博客园上看到很多人讨论的”在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 { get; set; }
/// <summary>
/// 第几代
/// </summary>
public int Generation { get; set; }
/// <summary>
/// 唯一标识 id
/// </summary>
public int Id { get; set; }
/// <summary>
/// 母亲id
/// </summary>
public int ParentId { get; set; }
}
{
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 { get; set; }
/// <summary>
/// 第几代
/// </summary>
public int Generation { get; set; }
/// <summary>
/// 唯一标识 id
/// </summary>
public int Id { get; set; }
/// <summary>
/// 母亲id
/// </summary>
public int ParentId { get; set; }
}
2、生奶牛方法(非递归)

static List<Cow> listCows = new List<Cow>();
public static void GetBirth(int year)
{
List<Cow> listBornCows = new List<Cow>();//用于添加新生的奶牛
Cow firstCow = new Cow(1, 1, 1, 0);
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 void GetBirth(int year)
{
List<Cow> listBornCows = new List<Cow>();//用于添加新生的奶牛
Cow firstCow = new Cow(1, 1, 1, 0);
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));
}
}
}
}
{
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(1, 1, 1, 0);
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();
}
}
{
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(1, 1, 1, 0);
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/
本文版权归作者和博客园共有,欢迎围观转载。转载时请您务必在文章明显位置给出原文链接,谢谢您的合作。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构