惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
G
Google Developers Blog
量子位
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
B
Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
美团技术团队
V
Visual Studio Blog
Last Week in AI
Last Week in AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Fortinet All Blogs
博客园 - Franky
The Register - Security
The Register - Security
O
OpenAI News
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
罗磊的独立博客
博客园 - 叶小钗
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
SecWiki News
SecWiki News
T
Tor Project blog
月光博客
月光博客
S
Secure Thoughts
博客园 - 【当耐特】
Help Net Security
Help Net Security
D
Docker
Recent Announcements
Recent Announcements
GbyAI
GbyAI
B
Blog RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
V
Vulnerabilities – Threatpost
Forbes - Security
Forbes - Security
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Hacker News: Ask HN
Hacker News: Ask HN
Cyberwarzone
Cyberwarzone
宝玉的分享
宝玉的分享
Cisco Talos Blog
Cisco Talos Blog
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog

博客园 - Edison.Feng

Objective-读Xml [转]iPhone Mapkit 之在地图加入坐标点 使用MKAnnotation和MKAnnotationView [转]Android:使用URL和URLConnection(多线程下载) 【转】httpModules 与 httpHandlers 【转】挣脱浏览器的束缚(2) - 别让脚本引入坏了事 [转]asp.net中利用ashx实现图片防盗链 求任何一个正数的组合,组合的规则是这个数等于1或2的整数幂之和,请列出组合的情况。 C#利用反射获取对象属性的修改情况 C#利用反射获取对象属性值 Clone:Xml序列化反序列克隆对象 [转]Linux slab 分配器剖析 [转]Linux对I/O端口资源的管理 [转]linux内核分析-初始化分析 start_kernel paging_init [转]Linux TCP/IP 协议栈的关键数据结构Socket Buffer(sk_buff ) [转]Linux系统内核接收以太帧的处理程序 MS SQL:Funcation拆分行 我使用WebService:Function.asmx的惊叹!!!
母牛生小牛(递归)
Edison.Feng · 2008-09-04 · via 博客园 - Edison.Feng

问题:

  有一个农场有一头成年母牛,每三个月后一头小牛,小牛一年后长大,长大后每三个月又可以生一头小牛,如此循环,问10年后农场一共有多少牛? (假设牛都是母牛)

分析: 

   1.每年有12个月,母牛每三个月可生小牛,可见生小牛的频率可以抽象成每年4个,10年就是40个单位.

   2.通过穷举每只母牛40个单位里可生小牛总数,其总和便是总牛数.

   3.小牛可以生小需要5个单位的成长期.

实现:

   再抽取出关键的数字后,算法怎么实现呢?

    1.第一只母牛,10年显然可生40只小牛.

   2.这第一代的40只小牛,在40个单位里又能生多少只小牛呢?可以很快推算出40只里最大的牛拥有生育能力比起母类要少5个单位,即可生35只,第二大的是34 ,依次类推,最后是1.这显然是35的阶乘.假设Num为牛的总算的记录,那么算法可以现实为:

     for(int i = 35 ; i > 0 ; i++) Num += i;

这样便计算出了第二代子牛的总数,那第三代该如何计算呢?同样第二代的35只需要类似第代的40只计算子代的方法一样,第二代的34也是如此,那么N只牛对应的子代计算的方法可以总结为

    for(int i = n - 5; i > 0 ; i++) Num += i;

到此可以总结出:每代子牛对应的直接子牛总数参与上叙迭代后的Num.

具体实现:

   static void CreateCattle(int n, ref int num)

        {

            int next = n - 5;

            for (int i = next; i > 0; i--)

            {

                num += i; //第二代

                CreateCattle(i, ref num); //第三代之后

            }

        } 

可以发现,上叙递归的结果Num没有加进去第一代和第零代的母牛.所以需要初始化Result = 41;

         int result = 41;

         CreateCattle(40, ref result);

全部代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Cattle

{

    class Program

    {

        static void Main(string[] args)

        {

            int result = 41;

            CreateCattle(40, ref result);

            Console.WriteLine("The cattles's sum is : " + result);

            Console.ReadLine();

        }

        static void CreateCattle(int n, ref int num)

        {

            int next = n - 5;

            for (int i = next; i > 0; i--)

            {

                num += i;

                CreateCattle(i, ref num);

            }

        }

    }

}

续:这里假定的是10年,算法可以延生到N年.