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

推荐订阅源

Engineering at Meta
Engineering at Meta
P
Privacy International News Feed
W
WeLiveSecurity
Spread Privacy
Spread Privacy
S
Schneier on Security
Google Online Security Blog
Google Online Security Blog
N
News and Events Feed by Topic
Forbes - Security
Forbes - Security
Cisco Talos Blog
Cisco Talos Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
Lohrmann on Cybersecurity
P
Privacy & Cybersecurity Law Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
人人都是产品经理
人人都是产品经理
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
月光博客
月光博客
博客园_首页
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
www.infosecurity-magazine.com
www.infosecurity-magazine.com
AWS News Blog
AWS News Blog
WordPress大学
WordPress大学
AI
AI
酷 壳 – CoolShell
酷 壳 – CoolShell
Hacker News: Ask HN
Hacker News: Ask HN
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
P
Proofpoint News Feed
The Hacker News
The Hacker News
The Cloudflare Blog
Vercel News
Vercel News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cloudbric
Cloudbric
C
Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
I
Intezer
Jina AI
Jina AI
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
Microsoft Azure Blog
Microsoft Azure Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
B
Blog

博客园 - 西门潇洒

MSSQL批量写入数据方案 关于SQL连接 加条件查询的LINQ表达式写法 关于LINQ中实现随机查询数据库中记录 .NET新特性--扩展方法 Array中数据强制数据类型转换 the Project type is not supported by installation 项目类型不能正确加载 ASP.NET三层架构中数据绑定的问题 ASP.NET 2.0中XML数据的处理 <转>自毁前程的七种行为 <转>在 ASP.NET 中执行 URL 重写 正则式中的实用命名组替换 去除socket编程当中接收到的多余符\0 - 西门潇洒 - 博客园 将一段符合XML格式规范字符串插入已有XML文档当中 <转>如何C#中实现在TreeView查找某一节点 javascript正则表达式中使用变量关键字 <转>JavaScript 参考教程之对象化编程 <转>JavaScript 参考教程之JavaScript 简介 <转>JavaScript 参考教程之事件处理 <转>JavaScript 参考教程资料之文档对象
小牛生产小牛的问题解决集粹
西门潇洒 · 2008-02-19 · via 博客园 - 西门潇洒

问题:
          一只刚出生的小牛,4年后生一只小牛,以后每年生一只。现有一只刚出生的小牛,问N年后共有牛多少只?

1.原始笨方法

private int Comput(int years)
        
{
            
//初始化为1头牛
            int count = 1;
            
if (years < 4)
          
{
                
return count;
            }

            
            
while (years - 3 > 0)
            
{
                count 
= count + Comput(years - 3);
                years
--;
            }

            
return count;


        }

特点:容易理解,不过效率太低.不具有实用价值.

2.采用HashTable优化方案

Hashtable table = new Hashtable();
        
public long Compute(int years)
        
{
            
//初始化为1头牛
            long count = 1;
            
if (years <= 3)
          
{
                
return count;
            }

            
int i = 4;
            
while (i <= years)
            
{
                
int subYears = i - 3;
                
if (table.ContainsKey(subYears))
              
{
                    count 
= (long)table[subYears];
                }

                
else
              
{
                    count 
+= Compute((int)(subYears));
                }

                
if (!table.ContainsKey(subYears))
              
{
                    table.Add(subYears, count);
                }

                i
++;
            }

            
return (long)count;
        }

特点:在第一种方案的基础上性能有了大幅度的提高.采用HashTable存储老牛的生育曲线,从而达到以后的小牛重复利用老牛的生育曲线.(直接取其生产数量)

3.采用数组的方式描述

public int Compute(int years)
        
{
          
int[] age = new int[41000 };
            
for (int i = 2; i <= years; i++)
            
{
                age[
3+= age[2];              age[2= age[1]; 
                age[
1= age[0];
                age[
0= age[3];                 

            }

            
return (age[0+ age[1+ age[2+ age[3]);
        }

特点:只采用一个循环搞定,效率极高.

3.采用优化递归公式实现
 f(n)   =   f(n-1)+f(n-3)  [n   >   3]   
 f(n)   =   1                   [0   <   n   <=   3]  

public int Comput(int x)
      
{
            
if (x < 4return 1;
            
else return Comput(x - 1+ Comput(x - 3);
        }
   

特点:代码简洁,功能简单实现,但使用递归当然会牺牲一定的效率作为代价.

前些天在网络上偶然发现的生产小牛问题.于是搜集整理了一下,方便大家共同研究学习使用.
有好的算法大家共同研究^_^