package com.xiaowu.demo;

//    有一只兔子,从出生后第3个月起每个月都生一只兔子,小兔子长到第四个月后每个月又生一只兔子,假如兔子都不死,问每个月的兔子总数为多少?


/**
 * 
 * @author WQ
 *
 */


public class Demo1 {

    public static void main(String[] args) {
        for(int i = 1;i<20;i++){
            int a = i;
            int sum = f(a);
            System.out.println("第" + a + "个月的兔子数为:" + sum);
        }
    }

    public static int f(int n) {
        int sum = 0;
        if (n == 1 || n == 2) {
            sum = 1;
        } else {
            sum = f(n - 1) + f(n - 2);
        }
        return sum;
    }

}

 

相关文章:

  • 2022-12-23
  • 2021-11-05
  • 2021-12-22
  • 2022-02-21
  • 2021-11-14
  • 2021-07-29
猜你喜欢
  • 2021-06-23
  • 2022-01-29
  • 2022-12-23
  • 2021-10-29
  • 2021-11-19
  • 2021-08-13
  • 2021-04-15
相关资源
相似解决方案