一个牧场目前一共有20头刚出生的羊,母羊、公羊各一半。假如母羊5岁时后每年生一胎(母羊,公羊各一半)。羊活到10岁后死亡。请问20年后这个牧场有多少只羊? 请用C#写出算法。
2016-05-17 12:25
543 查看
假设一胎生一只公羊和一只母羊。
实现代码:
实现代码:
using System; using System.Collections.Generic; using System.Text; namespace 多少只羊 { class Program { static void Main(string[] args) { int sheepCount; for (int i = 1; i <= 20; i++) { sheepCount = GetSheepCount(i) - GetDeathSheepCount(i); Console.WriteLine(sheepCount); } } private static int GetSheepCount(int year) { if (year <= 4) return 20; return GetSheepCount(year - 1) + GetSheepCount(year - 5) - GetDeathSheepCount(year); } private static int GetDeathSheepCount(int year) { if (year < 10) return 0; return GetSheepCount(year - 10); } } }
相关文章推荐
- C#清除WebBrowser中Cookie缓存的方法
- 第1章 C#概述
- WPF依赖属性
- C#日期时间格式化
- C#中路由事件的使用
- 使用C#解析XMIND文件格式
- c#之xml
- c#解压压缩文件和文件夹
- 用C#一步步写串口通信
- c#泛型使用详解:泛型特点、泛型继承、泛型接口、泛型委托
- C# 爬虫 100个明星贴吧
- C#中泛型方法与泛型接口
- C# default关键字的使用方法
- C# 请求外部服务的办法
- C# url 路径转换 相对路径 转换为 绝对路径
- C#获取当前系统磁盘符、系统目录、桌面等
- C#中如何获取系统环境变量等
- C#的Excel导入、导出
- C# windows窗体间的传值
- 如何用C#获得文件信息以及扩展信息