一个有趣的算老鼠程序

          昨天一个同事在玩一个GameBoy小智力游戏时间碰到一数学题,于是拿出来大家讨论。
题目是这样的:说老鼠的生育能力惊人,一只老鼠假定它不需要交配的话一次能生20只小老鼠。隔2个月又能再生一窝。问现有一只老鼠刚生完一窝,十个月后这个老鼠窝里会有几只老鼠?当然,这只是一道IQ题。真要算也有不下几十种算法。用中学的方程式也很好解。然而我们是做程序的,能不能就写一个程序来计算一下呢?当然没有问题,以下就我写的全部代码:
None.gifusing System;
None.gifusing System.Collections.Generic;
None.gifusing System.Text;
None.gif
None.gifnamespace Test
ExpandedBlockStart.gif ContractedBlock.gif dot.gif{
InBlock.gif   public class 老鼠
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif       public 老鼠()
ExpandedSubBlockStart.gif ContractedSubBlock.gif        dot.gif{          
ExpandedSubBlockEnd.gif       }
InBlock.gif
InBlock.gif       public 老鼠(鼠窝 出生地)
ExpandedSubBlockStart.gif ContractedSubBlock.gif        dot.gif{
InBlock.gif           _我的鼠窝 = 出生地;
ExpandedSubBlockEnd.gif       }
InBlock.gif
InBlock.gif        private int _月龄;
InBlock.gif        private int _生育能力;
InBlock.gif        private 鼠窝 _我的鼠窝;
InBlock.gif
InBlock.gif        public int 月龄
ExpandedSubBlockStart.gif ContractedSubBlock.gif         dot.gif{
InBlock.gif            get
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                return _月龄;
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            set
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                _月龄 = value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        public int 生育能力
ExpandedSubBlockStart.gif ContractedSubBlock.gif         dot.gif{
InBlock.gif            get
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                return _生育能力;
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            set
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                _生育能力 = value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif       public 鼠窝 我的鼠窝
ExpandedSubBlockStart.gif ContractedSubBlock.gif        dot.gif{
InBlock.gif           get
ExpandedSubBlockStart.gif ContractedSubBlock.gif            dot.gif{
InBlock.gif               return _我的鼠窝;
ExpandedSubBlockEnd.gif           }
InBlock.gif
InBlock.gif           set
ExpandedSubBlockStart.gif ContractedSubBlock.gif            dot.gif{
InBlock.gif               _我的鼠窝 = value;
ExpandedSubBlockEnd.gif           }
ExpandedSubBlockEnd.gif       }
InBlock.gif
InBlock.gif
InBlock.gif        public void 生小老鼠()
ExpandedSubBlockStart.gif ContractedSubBlock.gif         dot.gif{
InBlock.gif            for (int i = 0; i < _生育能力; i++)
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                老鼠 新生鼠 = new 老鼠(_我的鼠窝);
InBlock.gif                新生鼠.月龄 = 0;
InBlock.gif                新生鼠.生育能力 = _生育能力;
InBlock.gif
InBlock.gif                _我的鼠窝.添加新成员(新生鼠);
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif
None.gifusing System;
None.gifusing System.Collections.Generic;
None.gifusing System.Text;
None.gif
None.gifnamespace Test
ExpandedBlockStart.gif ContractedBlock.gif dot.gif{
InBlock.gif    public class 鼠窝
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif        public 鼠窝()
ExpandedSubBlockStart.gif ContractedSubBlock.gif         dot.gif{
InBlock.gif            _老鼠大家庭 = new List<老鼠>();
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        private List<老鼠>  _老鼠大家庭;
InBlock.gif        public List<老鼠> 老鼠大家庭
ExpandedSubBlockStart.gif ContractedSubBlock.gif         dot.gif{
InBlock.gif            get
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                return _老鼠大家庭;
ExpandedSubBlockEnd.gif            }  
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        public int 老鼠数量
ExpandedSubBlockStart.gif ContractedSubBlock.gif         dot.gif{
InBlock.gif            get 
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                return _老鼠大家庭.Count;
ExpandedSubBlockEnd.gif            }            
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        public void 添加新成员(老鼠 小鼠宝宝)
ExpandedSubBlockStart.gif ContractedSubBlock.gif         dot.gif{
InBlock.gif            _老鼠大家庭.Add(小鼠宝宝);
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        public void 告别成员(老鼠 鼠列士)
ExpandedSubBlockStart.gif ContractedSubBlock.gif         dot.gif{
InBlock.gif            _老鼠大家庭.Remove(鼠列士);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif
None.gifusing System;
None.gifusing System.Collections.Generic;
None.gifusing System.Text;
None.gif
None.gifnamespace Test
ExpandedBlockStart.gif ContractedBlock.gif dot.gif{
InBlock.gif    class Program
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif        private static int 月份 = 3;
InBlock.gif        private static int 每次可生数量 = 1;
InBlock.gif
InBlock.gif        static void Main(string[] args)
ExpandedSubBlockStart.gif ContractedSubBlock.gif         dot.gif{
InBlock.gif            鼠窝 鼠的乐园 = new 鼠窝();
InBlock.gif            老鼠 创始鼠 = new 老鼠(鼠的乐园);
InBlock.gif            创始鼠.月龄 = 2;
InBlock.gif            创始鼠.生育能力 = 每次可生数量;
InBlock.gif            鼠的乐园.添加新成员(创始鼠);
InBlock.gif
InBlock.gif            for (int i = 1; i <= 月份; i++)
ExpandedSubBlockStart.gif ContractedSubBlock.gif             dot.gif{
InBlock.gif                for (int k = 0; k < 鼠的乐园.老鼠数量; k++)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif{
InBlock.gif                    鼠的乐园.老鼠大家庭[k].月龄++;
ExpandedSubBlockEnd.gif                }
InBlock.gif
InBlock.gif                for (int j = 0; j < 鼠的乐园.老鼠数量;j++ )
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif{
InBlock.gif                    老鼠 阿鼠 = 鼠的乐园.老鼠大家庭[j];
InBlock.gif                    if (阿鼠.月龄 >= 2)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif{
InBlock.gif                        阿鼠.生小老鼠();
ExpandedSubBlockEnd.gif                    }
ExpandedSubBlockEnd.gif                }                
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            System.Console.WriteLine(鼠的乐园.老鼠数量.ToString());
InBlock.gif
InBlock.gif            System.Console.ReadLine();
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif

程序写完,其实这样的程序很普通,不就是个简单的C#程序吗?但我回想起我们现在的工作,写了很多企业级应用一直在鼓吹面向对象却连这样的小程序都不如。后台封装好的数据访问组件供我们调用,前台代码依旧那样的老套,开发模式依然没有创新。为此我们应该反省......

转载于:https://www.cnblogs.com/Nelson/archive/2007/06/17/786675.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值