黑马程序员技术交流社区

标题: 提问,大家帮我解答一下,怎么写?给个思路也行 [打印本页]

作者: mysouffle    时间: 2014-8-22 22:23
标题: 提问,大家帮我解答一下,怎么写?给个思路也行
有一对兔子,从出生后第3个月起每个月都生一对兔子,
小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,
问第20个月的兔子对数为多少?
作者: ┣┫流枫    时间: 2014-8-22 22:30
和裴波那契数列一样    当前项是前两项的和   第一二项为1
作者: hejinzhong    时间: 2014-8-22 22:30

  1. 这个是循环打印每个月的,如果要20,就不要循环,直接传个20过去
  2. 1.程序分析:   兔子的规律为数列1,1,2,3,5,8,13,21....  
  3. public class exp2{
  4.         public static void main(String args[]){
  5.                 int i=0;
  6.                 for(i=1;i<=20;i++)
  7.                         System.out.println(f(i));
  8.         }
  9.         public static int f(int x)
  10.         {
  11.                 if(x==1 || x==2)
  12.                         return 1;
  13.                 else
  14.                         return f(x-1)+f(x-2);
  15.         }
  16. }

  17. public class exp2{
  18.         public static void main(String args[]){
  19.                 int i=0;
  20.                 math mymath = new math();
  21.                 for(i=1;i<=20;i++)
  22.                         System.out.println(mymath.f(i));
  23.         }

  24. }
  25. class math
  26. {
  27.         public int f(int x)
  28.         {
  29.                 if(x==1 || x==2)
  30.                         return 1;
  31.                 else
  32.                         return f(x-1)+f(x-2);
  33.         }
  34. }
复制代码


作者: TheBest__^    时间: 2014-8-23 20:47
楼上正解
作者: lhtwm1    时间: 2014-8-23 21:27
这是 哪段视频阶段的题哦
作者: mysouffle    时间: 2014-8-23 21:57
lhtwm1 发表于 2014-8-23 21:27
这是 哪段视频阶段的题哦

没看视频,不知道视频上有木有
作者: mysouffle    时间: 2014-8-23 22:00
谢谢大家解答,感激不尽啊
作者: 小黑子    时间: 2014-8-23 22:12
月份    兔子对数
1            1
2            1
3            2
4            3
5            5
6            8
7            13
  1. /*
  2. 思想:
  3.         从第2个月开始,上一个月是可产仔数量,本月是已有数量。
  4.         下月 = 本月 + 上月
  5. */
  6. class Fibonacci
  7. {
  8.         public int getFibonacci(int month)
  9.         {
  10.                 int f1,f2;
  11.                 f1=0;
  12.                 f2=1;
  13.                 if(month <= 0)
  14.                 {
  15.                         System.out.println("input error");
  16.                         return -1;
  17.                 }
  18.                 //因为已经设置第1个月,f2=1,故month=1无需运行for
  19.                 for(int x=1;x<month;x++)
  20.                 {
  21.                         f1 = f1 + f2;
  22.                         //交换结束后,始终保持f1为上月,f2为本月
  23.                         f1^=f2;
  24.                         f2^=f1;
  25.                         f1^=f2;
  26.                 }
  27.                 return f2;

  28.         }
  29. }
  30. class FibonacciDemo
  31. {
  32.         public static void main(String[] args)
  33.         {
  34.                 int num;
  35.                 num = new Fibonacci().getFibonacci(7);
  36.                 System.out.println("num = "+num);
  37.         }
  38. }
复制代码

其实很简单,LZ只要记住这个公式就OK了:
下月 = 本月 + 上月
作者: sunny~    时间: 2014-8-23 23:04
斐波拉契序列    一模一样的
作者: MeryStyle    时间: 2014-8-23 23:15
前两个月都是一对兔子,从第三个月开始,每个月兔子的对数都是之前相近两个月兔子对数的和。变样的斐波拉契数列,思路就是这样!
作者: mysouffle    时间: 2014-8-26 22:05
感受到了大家的热情




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2