题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?

程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21.... 

package Studytest;

public class Prog1 {
   public static void main(String [] args){
       int n = 30;
       System.out.println("第"+n+"个月兔子总数为"+fun(n));

   }
    private static int fun(int n){
        if(n==1 || n==2)
            return 1;
        else
            return fun(n-1)+fun(n-2);
    }

}

 

运行结果:

java 兔子生仔问题

 

相关文章:

  • 2021-11-24
  • 2022-12-23
  • 2021-09-27
  • 2021-12-29
  • 2022-01-13
  • 2022-02-26
  • 2021-10-17
猜你喜欢
  • 2022-03-04
  • 2022-12-23
  • 2022-02-09
  • 2021-08-22
  • 2022-02-27
  • 2022-12-23
相关资源
相似解决方案