CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

一道有趣的编程题,不知道对不对

楼主milkslzz(小肥虫.net)2005-05-11 10:06:24 在 C/C++ / C++ 语言 提问

题目是我在别的论坛上找的,因为没有标准答案,不知道对不对?请高手们指出问题  
   
  /*题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,  
  小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的  
  兔子总数为多少?  
  这个题目相信大家都不会陌生了,那如果每个免子只能活10个月呢?  
  */  
   
  #   include   <iostream.h>  
  //#   include   <windows.h>  
   
   
   
  void   rabbit(int   lifemonth,int   month)  
  {  
  static   int   num=1;                                         //设立静态变量,统计兔子个数  
   
   
  for(int   x=0;   x<lifemonth;   x++)               //循环10个月,看能生多少对  
  {  
  if(x>month)                                             //判断是否超过月数  
  break;  
  if(x>2)                                                     //第3个月开始生  
  {  
  num+=2;  
  rabbit(lifemonth,month-x);         //一只生一对,故调用2次  
  rabbit(lifemonth,month-x);  
  }  
  }  
           
  // system("cls");  
   
  cout   <<   "after   "   <<   month   <<   "   month,"    
  <<   "the   total   num   of   the   rabbit   is:   "   <<   num   <<   endl;            
  }  
   
  void   main()  
  {  
  int   lifemonth=10;                         //兔子可以存活10个月  
  int   month;                                       //计算多少个月后兔子多少  
  cout   <<   "please   input   the   month"   <<   endl;  
  cin   >>   month;  
                       
   
  rabbit(lifemonth,month);                                    
     
    } 问题点数:0、回复次数:32Top

1 楼lanzhu_like(蓝竹)回复于 2005-05-11 10:25:15 得分 0

(汗^^^)  
   
  一对兔子等于两只!一对兔子才生一对!  
   
  好不好??Top

2 楼milkslzz(小肥虫.net)回复于 2005-05-11 12:30:25 得分 0

一对兔子才生4   只阿  
  我用递归递归2次没错阿Top

3 楼sungengyu(快乐机器)回复于 2005-05-11 12:38:38 得分 0

这个用循环就可以了,没必要用递归。Top

4 楼jpq1982()回复于 2005-05-11 16:41:55 得分 0

thinking   is   right   perfectly,   but   maybe   the   using   of   x   had   some   problemsTop

5 楼velisheng(f)回复于 2005-05-11 16:48:11 得分 0

关键的是这样的   1   1  
                                                    2    
                                                    3  
                                                    5  
                                                    8  
                                                    13  
                                                    21  
  Top

6 楼binjuny(binjuny)回复于 2005-05-11 16:53:15 得分 0

是斐波数列吧  
  找老谭的书吧,上面有Top

7 楼nicknide(封月翔天)回复于 2005-05-11 17:26:56 得分 0

如果寿命只有10个月,可以设立循环队列,长度为10,然后记录每个月兔子的出生数量,当月份累计超过10之后,当前总数减去10月前的数量,让后再繁殖...  
   
  这个问题是有标准答案的...而且不复杂...Top

8 楼shiyunlong(君子爱财-取之用刀)回复于 2005-05-11 17:32:44 得分 0

生的小兔子  
  全为男性怎么办?Top

9 楼BCdoctor(BC博士)回复于 2005-05-11 17:43:49 得分 0

看看这样是不是好点:  
  #   include   <iostream.h>  
   
  void   rabbit(int   lifemonth,int   month)  
  {  
  if(month>10)   return;                                   //如果大于10个月咯屁  
   
  for(int   x=0;   x<month;   x++)                        
  {  
   
  if(x>=3)                                                     //第3个月开始生  
  {  
  num+=2;  
  rabbit(lifemonth,month-x);         //一只生一对,故调用2次  
  rabbit(lifemonth,month-x);  
  }  
  }  
           
  // system("cls");  
   
  cout   <<   "after   "   <<   month   <<   "   month,"    
  <<   "the   total   num   of   the   rabbit   is:   "   <<   num   <<   endl;            
  }  
   
  void   main()  
  {  
  int   lifemonth=10;                         //兔子可以存活10个月  
  int   month;                                       //计算多少个月后兔子多少  
                    static   int   num=1;                         //设立静态变量,统计兔子个数  
  cout   <<   "please   input   the   month"   <<   endl;  
  cin   >>   month;  
   
  rabbit(lifemonth,month);                                    
     
    }  
  Top

10 楼xdlhf(人在天涯)回复于 2005-05-11 17:58:41 得分 0

如果兔子全是一个性别的怎么办  
   
  汗一个先Top

11 楼jpq1982()回复于 2005-05-11 18:03:05 得分 0

to   :   BCdoctor(BC博士)    
  1.   better   at   exist   condition   processing,   but   "x>=3"   means   the   fourth   month,   as   x   start   from   0!!!!    
  2.   "if(month>10)"   the   dig   10   would   better   replaced   by   lifemonth.  
  :)Top

12 楼milkslzz(小肥虫.net)回复于 2005-05-11 18:10:49 得分 0

对哦   ,我还忘了兔子10个月就挂了,忘了   num的值   要少   1Top

13 楼xxwo(老K)回复于 2005-05-11 19:49:58 得分 0

哈哈,楼上写的太差,   我这个估计一般人也看不懂,不过效率是很高的,而且代码短的多,暂时只能计算到130多个月吧,有人看得懂我开帖子送分!  
   
  #include   <iostream>  
  #include   <cmath>  
  using   namespace   std;  
  const   double   fi=   (1.61803);  
   
  double   power(double   d,   int   n)  
  {  
  for(int   i=1;   i<=n;   ++i)  
  d   *=   fi;  
  return   d;  
  }  
   
  int   counterRabbit(int   iMonth)  
  {  
  int cnt;  
  if   (!iMonth)   return   1;  
  cnt   =   power(fi,   iMonth/3)/sqrt(5)   +   0.5   ;  
  return   cnt;  
  }  
   
  int   main()  
  {  
  for(int   i   =   0;   i   <   130   ;   i   +=   3)  
  cout   <<   "the   "   <<   i   <<"   month <-----> rabbit   count:   "   <<   counterRabbit(i)   <<   endl;  
   
  return   0;  
  }  
   
  运行结果如下:(基本正确吧,当前开始时间算0个月哦)  
   
  the   0   month <-----> rabbit   count:   1  
  the   3   month <-----> rabbit   count:   1  
  the   6   month <-----> rabbit   count:   2  
  the   9   month <-----> rabbit   count:   3  
  the   12   month <-----> rabbit   count:   5  
  the   15   month <-----> rabbit   count:   8  
  the   18   month <-----> rabbit   count:   13  
  the   21   month <-----> rabbit   count:   21  
  the   24   month <-----> rabbit   count:   34  
  the   27   month <-----> rabbit   count:   55  
  the   30   month <-----> rabbit   count:   89  
  the   33   month <-----> rabbit   count:   144  
  the   36   month <-----> rabbit   count:   233  
  the   39   month <-----> rabbit   count:   377  
  the   42   month <-----> rabbit   count:   610  
  the   45   month <-----> rabbit   count:   987  
  the   48   month <-----> rabbit   count:   1597  
  the   51   month <-----> rabbit   count:   2584  
  the   54   month <-----> rabbit   count:   4181  
  the   57   month <-----> rabbit   count:   6765  
  the   60   month <-----> rabbit   count:   10945  
  the   63   month <-----> rabbit   count:   17710  
  the   66   month <-----> rabbit   count:   28655  
  the   69   month <-----> rabbit   count:   46365  
  the   72   month <-----> rabbit   count:   75020  
  the   75   month <-----> rabbit   count:   121385  
  the   78   month <-----> rabbit   count:   196405  
  the   81   month <-----> rabbit   count:   317789  
  the   84   month <-----> rabbit   count:   514192  
  the   87   month <-----> rabbit   count:   831978  
  the   90   month <-----> rabbit   count:   1346166  
  the   93   month <-----> rabbit   count:   2178137  
  the   96   month <-----> rabbit   count:   3524291  
  the   99   month <-----> rabbit   count:   5702409  
  the   102   month <-----> rabbit   count:   9226669  
  the   105   month <-----> rabbit   count:   14929027  
  the   108   month <-----> rabbit   count:   24155614  
  the   111   month <-----> rabbit   count:   39084508  
  the   114   month <-----> rabbit   count:   63239906  
  the   117   month <-----> rabbit   count:   102324065  
  the   120   month <-----> rabbit   count:   165563406  
  the   123   month <-----> rabbit   count:   267886558  
  the   126   month <-----> rabbit   count:   433448488  
  the   129   month <-----> rabbit   count:   701332657  
   
   
   
   
   
   
  Top

14 楼xxwo(老K)回复于 2005-05-11 19:51:29 得分 0

不好意思,忘记了,只能活10个月,。。。,也没什么难的。   先回家吃饭了Top

15 楼milkslzz(小肥虫.net)回复于 2005-05-11 23:31:27 得分 0

你理解错了吧,那个兔子从出生开始的第3个月就开始生产了。。  
  输出应该是这样的:  
   
  the   0   month <-----> rabbit   count:   1  
  the   3   month <-----> rabbit   count:   3         //这里3个月后第一支兔子已经生了2只了  
  the   6   month <-----> rabbit   count:   13         //这里还是第一支兔子生,还有3个月时候的一对兔                                                                                             子又生了2支,一下子增加   2+2*2支兔子  
  …………  
   
   
  Top

16 楼milkslzz(小肥虫.net)回复于 2005-05-11 23:54:35 得分 0

修改了一下,这下应该没问题了。。  
   
  /*题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,  
  小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的  
  兔子总数为多少?  
  这个题目相信大家都不会陌生了,那如果每个免子只能活10个月呢?  
  */  
   
  #   include   <iostream.h>  
  static   int   rabbit_num=1;                                         //设立静态变量,统计兔子个数  
  int   rabbit(int   lifemonth,int   month)  
  {  
   
  int   null;  
   
  for(int   x=0;   x<lifemonth;   x++)               //循环10个月,看能生多少对  
  {  
  if(x>month)                                             //判断是否超过月数  
  break;  
  if(x>2)                                                     //第3个月开始生  
  {  
  rabbit_num+=2;  
  null=rabbit(lifemonth,month-x);         //一只生一对,故调用2次  
  null=rabbit(lifemonth,month-x);  
  }  
  }  
  if(month>10)                                                         //增加了这个,兔子活了10个月就挂了  
  rabbit_num--;  
           
            return   rabbit_num;          
  }  
   
  void   main()  
  {  
  int   lifemonth=10;                         //兔子可以存活10个月  
  int   month;                                       //计算多少个月后兔子多少  
  cout   <<   "please   input   the   month"   <<   endl;  
  cin   >>   month;  
   
  int   num;                      
   
  for(int   x=1;   x<=month;   x++)  
  {  
  num=rabbit(lifemonth,x);        
  rabbit_num=1;                         //循环计算n个月后重新初始化兔子个数  
  cout   <<   "after   "   <<   x   <<   "   month,"    
  <<   "the   total   num   of   the   rabbit   is:   "   <<   num   <<   endl;    
  }  
    }  
   
  Top

17 楼jpq1982()回复于 2005-05-12 09:34:34 得分 0

the   0   month <-----> rabbit   count:   1  
  the   3   month <-----> rabbit   count:   1  
  the   6   month <-----> rabbit   count:   2  
  the   9   month <-----> rabbit   count:   3  
  the   12   month <-----> rabbit   count:   5  
   
  is   it   right??:)Top

18 楼BabySky716(吃睡等死)回复于 2005-05-12 09:38:03 得分 0

兔子有没有雌雄啊?是不是每个兔子都能生。。。。Top

19 楼milkslzz(小肥虫.net)回复于 2005-05-12 13:46:14 得分 0

the   0   month <-----> rabbit   count:   1  
  the   3   month <-----> rabbit   count:   1  
  the   6   month <-----> rabbit   count:   2  
  the   9   month <-----> rabbit   count:   3  
  the   12   month <-----> rabbit   count:   5  
   
  这样肯定错啦,注意看清楚题目呀~~  
  要是acm的题这样审题那不就忘了。。。。Top

20 楼milkslzz(小肥虫.net)回复于 2005-05-12 13:52:38 得分 0

兔子不管是雌雄,都能生好了。  
  是雄的让他自交好了。。。。。。^_^Top

21 楼withc(zp')回复于 2005-05-12 16:16:20 得分 0

都出现色情成分!!!!!!Top

22 楼sway2003009(sway2003009)回复于 2005-05-15 20:14:22 得分 0

我的答案,大家分析一下看。^_^  
  //   rabbit_problem.cpp   :   Defines   the   entry   point   for   the   console   application.  
  //  
   
  #include   "stdafx.h"  
  #include   <iostream.h>  
   
  const   int   LIFE   =   10;  
   
  int   rabit(int   month)  
  {  
  int   n   =   month/3;  
  int   num   =   1;  
  bool   flag   =   false;  
  for(   int   i   =0;   i   <   n;   i++)  
  {  
  if((i+1)*3   >=   10)  
  {  
  flag   =true;  
  num   =   num   -1;  
  }  
  num   =   3*num;  
  }  
  if(!flag)  
  return   (num   +1);  
  else  
  return   num;  
  }  
  int   main(int   argc,   char*   argv[])  
  {  
  int   month;  
  cout   <<"请输入月份数目:";  
  cin   >>   month;  
  int   number;  
  number   =   rabit(12);  
  for(int   i   =1;   i<=month;   i++)  
  {  
  number   =   rabit(i);  
  cout   <<"第"   <<   i   <<   "个月的兔子数为:\t"   <<   number   <<"只"   <<endl;  
  }  
  return   0;  
  }  
  Top

23 楼zhongwei5695(威少求学C++)回复于 2005-05-15 20:49:42 得分 0

只有两只兔子.........兔子要绝种了.....嘎嘎  
  哭哦..........以后没有兔子肉吃了..........Top

24 楼milkslzz(小肥虫.net)回复于 2005-05-16 12:59:10 得分 0

晕哦,能不能写点注释阿,这样别人看的好辛苦哦  
   
  要知道注释也是程序的一部分哦~~Top

25 楼awperpvip(挖靠我狂晕来CSDN这么久,竟不知道原来名字可以这么长)回复于 2005-05-16 13:07:42 得分 0

有意思了Top

26 楼krh2001(边城浪子)回复于 2005-05-16 13:53:26 得分 0

请问每只兔子都是母的吗?   没有公的母的怎么会怀孕?Top

27 楼yangjundeng(天下无双)回复于 2005-05-16 14:24:30 得分 0

全是近亲繁殖!!!汗Top

28 楼Salam2001(Upgrading : C++ and Data Structure ...)回复于 2005-05-16 14:34:23 得分 0

回复人:krh2001(边城浪子)   (   二级(初级))   信誉:100   2005-05-16   13:53:00   得分:0  
  ?    
  请问每只兔子都是母的吗?   没有公的母的怎么会怀孕?  
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
   
  人工授精嘛,都什么年代了都,不用兔爸爸费心了   ...Top

29 楼FromNoWhere()回复于 2005-05-16 15:10:14 得分 0

题目描述:  
   
  若一头小母牛,从出生起第四个年头开始每年生一头母牛,按此规律,第n年有多少头母牛?  
   
  自己写的:  
  /**********************************/  
    a[]   保存每年的新生母牛数目    
    a[n]=   a[1]+   a[2]   +   a[3]   +....+a[n-3]    
    最后将前n年母牛数目加起来就得到了n年母牛的总数目  
  /*********************************/  
  int   _tmain(int   argc,   _TCHAR*   argv[])  
  {  
   
  int   year=1,   total=0;  
  memset(a,   sizeof(a),   0);  
  a[1]=1;  
  cout<<"intput   the   year:"<<endl;  
  cin>>year;  
  a[4]=1;  
  for(int   i=5;   i<=year;   ++i)  
  {  
  for(int   j=1;   j<=(i-4)+1;   ++j)  
  {  
  a[i]+=a[j];  
  }  
  }  
  for(int   i=1;   i<=year;   ++i)  
  {  
  total+=a[i];  
  }  
  cout<<"the   total   number   is:   "<<total<<endl;  
  return   0;  
  }  
   
  网上的:  
  1,   利用   f(n)=   f(n-1)   +   f(n-3)   得到第n年的母牛数目    
  long   num_cow(int   n){    
        return   (n   <   4)   ?   1   :   num_cow(n-1)   +   num_cow(n-3);    
  }    
   
  或者:  
  for(i=3;i<100;i++)    
  f[i]=f[i-1]+f[i-3];  
   
  2,  
  利用STL的accumulate(累加)算法简化代码.    
   
    bool   cow_number(vector<int>&   vec,int   number)    
  {    
            if(number>100||number<0)return   false;    
            else{    
                      for(int   n=vec.size();n<number;n++)    
                                vec.push_back(accumulate(vec.begin(),vec.end()-2,0));    
                      return   true;                          
                    }//end   else    
            return   true;    
  }//end   cow_number(......    
   
  int     main()    
  {    
      int   a[]={1,0,0,1};    
      int   flag=0;    
      vector<int>   temp_vec(a,a+4);    
      cout<<"请输入年龄数"<<endl;    
   
      cin>>flag;    
      cow_number(temp_vec,flag);    
      cout<<"今年牛的中数是:";    
      cout<<accumulate(temp_vec.begin(),temp_vec.begin()+flag,0)<<endl;    
      system("pause");    
      return   0;    
  }//end   int   main()    
  Top

30 楼hell_wolf(浪花)回复于 2005-05-16 15:19:37 得分 0

成年兔子每月都会生一对,但怎么全是3   6   9...,难道4,5,7,8...月没兔子吗?Top

31 楼milkslzz(小肥虫.net)回复于 2005-05-16 21:40:45 得分 0

回复人:   hell_wolf(浪花)   (   )   信誉:100     2005-05-16   15:19:00     得分:   0      
     
     
        成年兔子每月都会生一对,但怎么全是3   6   9...,难道4,5,7,8...月没兔子吗?  
       
    ---------------------------------------  
  我是楼主,我写的程序没这个问题哦?     我写的那个都每个月都有生的,而且成年兔子只能活10个月。到11个月后那只兔子死亡,兔子总数num减少一个Top

32 楼milkslzz(小肥虫.net)回复于 2005-05-16 21:43:06 得分 0

兔子不分男女,都用clong好了。这样就没有性别歧视了。。。。  
   
  clong生殖,忽略男女因素。这样3个月后兔子开始克隆。。。。。  
   
  在不行男的搞玻璃生产好了   。。。。。。。。。。  
  女的也搞。。。   +_+~~Top

相关问题

  • 一道编程题
  • 一道编程题
  • 一道很有趣的VC编程问题,看了绝对不会后悔......
  • 一道编程题 ,(高分)
  • 求解一道编程题
  • 一道编程题目
  • 请问:一道编程题
  • 请教一道编程题
  • 请问一道编程题?
  • 高手请进:一道编程题

关键词

  • null
  • 兔子
  • month rabbit count
  • lifemonth
  • rabbit
  • 月
  • month
  • 题目
  • num
  • 故调用2次

得分解答快速导航

  • 帖主:milkslzz

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo