| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2061 人关注过本帖
标题:[求助]C++题目
只看楼主 加入收藏
cilubutong
Rank: 1
等 级:新手上路
帖 子:119
专家分:2
注 册:2007-5-22
结帖率:75%
收藏
 问题点数:0 回复次数:40 
[求助]C++题目
今天晚上老师给我们一个题目:
有一个农场,有一头小母牛,它到了四岁的时候就可以生一头小牛,以后的每年都可以生一头,到了十三年后这这头可以生多少头牛,(它生的小年到了四年的时候也可以生小牛)
希望师兄们给我点意见并且最好给点注释,因为老师会问我这步是干什么的?
搜索更多相关主题的帖子: 小牛 干什么 最好 
2007-06-12 22:30
cilubutong
Rank: 1
等 级:新手上路
帖 子:119
专家分:2
注 册:2007-5-22
收藏
得分:0 

生的小牛都是母牛.


全国最大的网上服装批发[url]www.[/url]
2007-06-12 22:31
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 

/*---------------------------------------------------------------------------
File name: bccn-Heifer-right.cpp
Author: HJin
Date: 6/13/2007
*/

/**
Recurrence formula for f(n), where n is the year
number. Now is year 1.

f(n) = f(n-3) + f(n-1), if n>=4
= 1, if n<=3

This formula is very similar to the famous Fibonacci formula.

One can solve this recurrence equation mathematically, using the
three roots, x_1, x_2, x_3, of
x^3 - x^2 -1 = 0.
You can solve the cubic equation using Cardano formula:
x_1 ~ 1.47, x_2 ~-0.23 + 0.79 i, x_3 = complex conjugate of x_2


To compute it programatically, you can use aipb2007's recursive formula.
Or use my two formulae below.


Sample output:

Year No.| iterative | recursive
--------+-----------+-----------
1 | 1 | 1
2 | 1 | 1
3 | 1 | 1
4 | 2 | 2
5 | 3 | 3
6 | 4 | 4
7 | 6 | 6
8 | 9 | 9
9 | 13 | 13
10 | 19 | 19
11 | 28 | 28
12 | 41 | 41
13 | 60 | 60
14 | 88 | 88
15 | 129 | 129
16 | 189 | 189
17 | 277 | 277
18 | 406 | 406
19 | 595 | 595
20 | 872 | 872
21 | 1278 | 1278
22 | 1873 | 1873
23 | 2745 | 2745
24 | 4023 | 4023
25 | 5896 | 5896
26 | 8641 | 8641
27 | 12664 | 12664
28 | 18560 | 18560
29 | 27201 | 27201
30 | 39865 | 39865
31 | 58425 | 58425
Press any key to continue . . .


*/


#include <iostream>
using namespace std;

/**
Iterative soln: time complexity is O(n); i.e., linear growth.
Space complexity is O(1).
*/
int f_iterative(int n);

/**
Recursive soln: time complexity is O(2^n); i.e., exponential growth.
*/
int f_recursive(int n);

int main()
{
int i;

cout<<"Year No.| iterative | recursive\n";
cout<<"--------+-----------+-----------\n";

for(i=1; i<32; ++i)
{
cout<<i<<"\t| "<<f_iterative(i)<<"\t | "<<f_recursive(i)<<endl;
}


return 0;
}


int f_iterative(int n)
{
int f0=1; // tracks f(n-3)
int f1=1; // tracks f(n-2)
int f2=1; // tracks f(n-1)
int res=1;
int i;

for(i=3; i<n; ++i)
{
res = f0+f2;

// update the tracking
f0 = f1;
f1 = f2;
f2 = res;
}

return res;
}

int f_recursive(int n) // simply the formula
{
if(n<=3)
return 1;
else
return f_recursive(n-3) + f_recursive(n-1);
}

[此贴子已经被作者于2007-6-14 2:09:54编辑过]


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-06-13 03:33
cilubutong
Rank: 1
等 级:新手上路
帖 子:119
专家分:2
注 册:2007-5-22
收藏
得分:0 
我们老师好象说答案是60头啊   你没把题目理解错误吧  老师说只有几个语句就可以了

全国最大的网上服装批发[url]www.[/url]
2007-06-13 10:01
cilubutong
Rank: 1
等 级:新手上路
帖 子:119
专家分:2
注 册:2007-5-22
收藏
得分:0 

那个大哥帮帮小弟吧!


全国最大的网上服装批发[url]www.[/url]
2007-06-13 12:10
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 

deleted. To save 1 byte of bc-cn web-space.

[此贴子已经被作者于2007-6-14 23:43:45编辑过]


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-06-13 12:27
cilubutong
Rank: 1
等 级:新手上路
帖 子:119
专家分:2
注 册:2007-5-22
收藏
得分:0 

小牛到了它四岁的时候就可以生小牛,它生的小牛到了四岁的时候也可以生小牛依次类推!


全国最大的网上服装批发[url]www.[/url]
2007-06-13 12:44
沉墨
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-6-12
收藏
得分:0 
回复:(cilubutong)小牛到了它四岁的时候就可以生小...
小牛现在多大,一岁?

/区区沉墨 /读过几年闲书 /尘世中一个迷途小书生.............
2007-06-13 15:43
zouxiaohua
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-5-28
收藏
得分:0 

先数学分析一下
30年后:
第一头可以产 13 - 3 = 10 头
第一头产的第一头小牛可以产: 13 - 8 = 5头
。。。。。。2 13 - 9 = 4
..........

2007-06-13 16:07
cilubutong
Rank: 1
等 级:新手上路
帖 子:119
专家分:2
注 册:2007-5-22
收藏
得分:0 

是的


全国最大的网上服装批发[url]www.[/url]
2007-06-13 18:38
快速回复:[求助]C++题目
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017825 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved