在博客园上看到很多人讨论的”在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();
}
}