题目是这样的:说老鼠的生育能力惊人,一只老鼠假定它不需要交配的话一次能生20只小老鼠。隔2个月又能再生一窝。问现有一只老鼠刚生完一窝,十个月后这个老鼠窝里会有几只老鼠?当然,这只是一道IQ题。真要算也有不下几十种算法。用中学的方程式也很好解。然而我们是做程序的,能不能就写一个程序来计算一下呢?当然没有问题,以下就我写的全部代码:
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
public class 老鼠
{
public 老鼠()
{
}
public 老鼠(鼠窝 出生地)
{
_我的鼠窝 = 出生地;
}
private int _月龄;
private int _生育能力;
private 鼠窝 _我的鼠窝;
public int 月龄
{
get
{
return _月龄;
}
set
{
_月龄 = value;
}
}
public int 生育能力
{
get
{
return _生育能力;
}
set
{
_生育能力 = value;
}
}
public 鼠窝 我的鼠窝
{
get
{
return _我的鼠窝;
}
set
{
_我的鼠窝 = value;
}
}
public void 生小老鼠()
{
for (int i = 0; i < _生育能力; i++)
{
老鼠 新生鼠 = new 老鼠(_我的鼠窝);
新生鼠.月龄 = 0;
新生鼠.生育能力 = _生育能力;
_我的鼠窝.添加新成员(新生鼠);
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Test
{
public class 老鼠
{
public 老鼠()
{
}
public 老鼠(鼠窝 出生地)
{
_我的鼠窝 = 出生地;
}
private int _月龄;
private int _生育能力;
private 鼠窝 _我的鼠窝;
public int 月龄
{
get
{
return _月龄;
}
set
{
_月龄 = value;
}
}
public int 生育能力
{
get
{
return _生育能力;
}
set
{
_生育能力 = value;
}
}
public 鼠窝 我的鼠窝
{
get
{
return _我的鼠窝;
}
set
{
_我的鼠窝 = value;
}
}
public void 生小老鼠()
{
for (int i = 0; i < _生育能力; i++)
{
老鼠 新生鼠 = new 老鼠(_我的鼠窝);
新生鼠.月龄 = 0;
新生鼠.生育能力 = _生育能力;
_我的鼠窝.添加新成员(新生鼠);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
public class 鼠窝
{
public 鼠窝()
{
_老鼠大家庭 = new List<老鼠>();
}
private List<老鼠> _老鼠大家庭;
public List<老鼠> 老鼠大家庭
{
get
{
return _老鼠大家庭;
}
}
public int 老鼠数量
{
get
{
return _老鼠大家庭.Count;
}
}
public void 添加新成员(老鼠 小鼠宝宝)
{
_老鼠大家庭.Add(小鼠宝宝);
}
public void 告别成员(老鼠 鼠列士)
{
_老鼠大家庭.Remove(鼠列士);
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Test
{
public class 鼠窝
{
public 鼠窝()
{
_老鼠大家庭 = new List<老鼠>();
}
private List<老鼠> _老鼠大家庭;
public List<老鼠> 老鼠大家庭
{
get
{
return _老鼠大家庭;
}
}
public int 老鼠数量
{
get
{
return _老鼠大家庭.Count;
}
}
public void 添加新成员(老鼠 小鼠宝宝)
{
_老鼠大家庭.Add(小鼠宝宝);
}
public void 告别成员(老鼠 鼠列士)
{
_老鼠大家庭.Remove(鼠列士);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
class Program
{
private static int 月份 = 3;
private static int 每次可生数量 = 1;
static void Main(string[] args)
{
鼠窝 鼠的乐园 = new 鼠窝();
老鼠 创始鼠 = new 老鼠(鼠的乐园);
创始鼠.月龄 = 2;
创始鼠.生育能力 = 每次可生数量;
鼠的乐园.添加新成员(创始鼠);
for (int i = 1; i <= 月份; i++)
{
for (int k = 0; k < 鼠的乐园.老鼠数量; k++)
{
鼠的乐园.老鼠大家庭[k].月龄++;
}
for (int j = 0; j < 鼠的乐园.老鼠数量;j++ )
{
老鼠 阿鼠 = 鼠的乐园.老鼠大家庭[j];
if (阿鼠.月龄 >= 2)
{
阿鼠.生小老鼠();
}
}
}
System.Console.WriteLine(鼠的乐园.老鼠数量.ToString());
System.Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Test
{
class Program
{
private static int 月份 = 3;
private static int 每次可生数量 = 1;
static void Main(string[] args)
{
鼠窝 鼠的乐园 = new 鼠窝();
老鼠 创始鼠 = new 老鼠(鼠的乐园);
创始鼠.月龄 = 2;
创始鼠.生育能力 = 每次可生数量;
鼠的乐园.添加新成员(创始鼠);
for (int i = 1; i <= 月份; i++)
{
for (int k = 0; k < 鼠的乐园.老鼠数量; k++)
{
鼠的乐园.老鼠大家庭[k].月龄++;
}
for (int j = 0; j < 鼠的乐园.老鼠数量;j++ )
{
老鼠 阿鼠 = 鼠的乐园.老鼠大家庭[j];
if (阿鼠.月龄 >= 2)
{
阿鼠.生小老鼠();
}
}
}
System.Console.WriteLine(鼠的乐园.老鼠数量.ToString());
System.Console.ReadLine();
}
}
}
程序写完,其实这样的程序很普通,不就是个简单的C#程序吗?但我回想起我们现在的工作,写了很多企业级应用一直在鼓吹面向对象却连这样的小程序都不如。后台封装好的数据访问组件供我们调用,前台代码依旧那样的老套,开发模式依然没有创新。为此我们应该反省......