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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - 玉米疯收

[C语言]排序问题--我的解答 转载:算法方面的一些书籍和网上资源 [C语言]排序问题 - 玉米疯收 - 博客园 如何利用css使PNG图片透明 - 玉米疯收 - 博客园 So, you think you know JavaScript? (你认为你懂JS吗) 不用密码使用ssh管理远程linux服务器 [转]mysql性能的检查和调优方法 PHP 中巧用数组降低程序的时间复杂度 HTTP协议中的5类状态码 冬日随笔 统计字符串中单词个数的算法优化 shell练习:svndiff & change_ip MYSQL的慢查询分析 csft安装过程中出错的问题及解决办法(目前仍然无法成功进行对中文的处理) 强烈鄙视“做成像……一样就可以了”这种需求被不负责任的提出 (转)关于程序员考研的研究 搭建PHP的缓存服务Memcache Gentoo用上虚拟机中的战斗机KVM 国外的程序员也这样!
牛年求牛。
玉米疯收 · 2009-12-14 · via 博客园 - 玉米疯收

今天在网上见到这么一道题,用任何语言实现都行。

题目为:有一母牛,到4岁可生育,每年一头,所生均是一样的母牛,到15岁绝育,不再能生,20岁死亡,问n年后有多少头牛?

玉丰补充: 此题有一个模糊的地方,就是牛的年龄问题,可以这样理解,从小牛出生的那一年开始计算,三年后就可以生小牛了,其数据如下:

1-3 A
4 A AB
5 A AB AC
6 A AB AC AD
7 A AB AC AD AE ABB(注意,今年AB已经4岁了,所以生了一只ABB)

AB 是 A 在第4年生的,到第7年时,它4岁了,所以也生了一只ABB

#include 
//隔几年生小牛
#define N 3
#define M 14
long ox_count = 1;
int main(){
    int i, n =69;
    printf("input a number(max 69)\n");
    scanf("%d",&n);

    long nums[101];
    for(i = 1 ; i<= n;i ++){
        if(i <= N ){
            nums[i] = 1;continue;
        }   
        if(i >= 20){
            //第二十年,我们伟大的母牛去了
            if(i == 20){
                nums[i] = nums[i - N] + nums[i -1] - 1 - nums[i -M];
            }   
            //第20到第20+N年间,没出生的牛,于是也没有死去的牛
            if(i > 20 && i <= 20 + N){ 
                nums[i] = nums[i - N] + nums[i -1]  - nums[i -M];
            }   
            //第20+N年后出生的牛数,要减去20年前的牛数.
            if(i > 20 + N){ 
                nums[i] = nums[i - N] + nums[i -1] - nums[i - 20 - N] - nums[i - M]; 
            }   
        }else{
            if(i < 15){
                //还没到20年后,所以没有死去的牛
                nums[i] = nums[i - N] + nums[i -1] ;
            }else{
                nums[i] = nums[i - N] + nums[i -1] -nums[i - M]; 
            }   
        }   
        printf("%d: %ld\n" ,i, nums[i]);
        ox_count = nums[i];
    }   
    printf("total: %ld\n" , ox_count);
}