| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2061 人关注过本帖
标题:[求助]C++题目
只看楼主 加入收藏
发发
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-6-13
收藏
得分:0 
算法很简单啊`从第4头开始以后每年能生牛的母牛的增加量=从第1年开始的总牛数

也就是 1 1 1 2 3 4 6 9.........
1 1 1 2 3 4 6 9.......
因为以后4年后每往后1年就有4年前出生的牛能够生育....
2007-06-14 00:15
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
/*
分析:
年:1 2 3 4 5 6 7 8 9 10 11 12 13
0 0 0 1 1 1 1 1 1 1 1 1 1 最初母牛每年产牛数
…………………………………………………………………
1 1 1 1 1 1 1 4年前母牛产牛数
1 1 1 1 1 1 ……………………
1 1 1 1 1 ……………………
2 2 2 2 ……………………
3 3 3 ……………………
4 4 ……………………
6 ……………………
注:年限对应数字仅表示当年新出生牛数
*/

#include <iostream>
using namespace std;

int compute(int year){
int a[year],count = 0,i;
//将最先的母牛在year年中每年生育牛数记录
for (i = 0;i < year;++i)
a[i] = (i < 3 ? 0 : 1); //第4年开始生

//主要步骤
for (i = 0;i < year;++i){
if (i > 3 && a[i-3] > 0){//a[i-3]表示第i年前4年出生的牛数
for (int j = i;j < year;++j)
a[j] += a[i-3]; //a[j]记录在第j年的产牛状况
}
}
//计算总牛数
for (i = 0;i < year;++i)
count += a[i];
return count + 1; //加上最初的母牛
}


int main(){
cout << compute(13) << endl;
system("pause");
return 0;
}

另一种思路。


我实在不明白楼上的意思
呵呵~迟钝了!

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


Fight  to win  or  die...
2007-06-14 09:40
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
aipb2007: Very good algorithm. You can use O(1) space insread. As you implemented it, your space reuirement is O(n).


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-06-14 10:49
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
回复:(HJin)aipb2007: Very good algorithm. You c...

What you said is "big O notation",right?

I know the concept but not clearly,can you show some example to describe how to compute it and use it.
So I can use this method to think abt program's complexity!

Thank you!


Fight  to win  or  die...
2007-06-14 12:33
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
回复:(HJin)aipb2007: Very good algorithm. You c...
I saw your new respond for this problem at 3f. Very good, as you said, it's really like fibnacci numbers.
It's so easy to understand fibnacci numbers, but when come to this problem,I never think abt that.

In addition, every time you solve a problem, you have a good style to write you analysis and detailed comment.Do you follow some templet or whatever?

Fight  to win  or  die...
2007-06-14 14:39
发发
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-6-13
收藏
得分:0 

int a[20]={1,1,1},i=0;
for(i>3;i<13;i++)
a[i]=a[i-1]+a[i-3];

a[12]就是要求的``60

我功底不行 看不懂复杂的 只会写这种~~~~~!

2007-06-14 18:41
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
以下是引用发发在2007-6-14 18:41:09的发言:

int a[20]={1,1,1},i=0;
for(i>3;i<13;i++)
a[i]=a[i-1]+a[i-3];

a[12]就是要求的``60

我功底不行 看不懂复杂的 只会写这种~~~~~!

You catch the main step of a good algorithm. Though you may not wrote the right code, however, what you thought is really good.
See the code on 3 floor, he has made the perfect program for that algorithm!


Fight  to win  or  die...
2007-06-14 22:02
Dam3000
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-6-5
收藏
得分:0 
这是一个类似 斐波那契数列 的东东
F[n]=F[n-1]+F[n-3] while(n>=4)
F[n]=1 while(n<4)
可以这么说吗?

看了21楼后得出的结论……

FORTRAN他爹说:要有高级语言 就有了烂熟的 Hello World!
2007-06-14 22:39
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
以下是引用aipb2007在2007-6-14 14:39:45的发言:
I saw your new respond for this problem at 3f. Very good, as you said, it's really like fibnacci numbers.
It's so easy to understand fibnacci numbers, but when come to this problem,I never think abt that.

In addition, every time you solve a problem, you have a good style to write you analysis and detailed comment.Do you follow some templet or whatever?

1. This problem is really a math problem. Do you agree?

2. There are some good C++/C coding style "standards" out there. I personally used some source code beautifier: say sourceFormatX (this program is very very very risky --- it may delete your windows registry if you use the shareware version!!! Google sourceFormatX and 看雪 for it), greatcode (open source @ sourceforge, and my own simple beautifier written in C++.

Best of the luck!


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

呵呵!小孤,我开始数了好几次也算错了,我们老师叫我们回去摆牙签!


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



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

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