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

public class test3 {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int n = sc.nextInt();
            int num = fun(n);
            System.out.println(num);
        }
    }

    public static int fun(int month){
        if(month < 5){
            return 1;
        }else{
            int num = 0;
            int a1 = 1;
            int a2 = 0;
            int a3 = 0;
            int a4 = 0;
            for(int i=2; i<=month; i++){
                num += a4;
                a4 = a3;
                a3 = a2;
                a2 = a1;
                a1 = num;
            }
            return num + a1 + a2 + a3 + a4;
        }
    }
}

 

相关文章:

  • 2022-12-23
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-04-17
  • 2021-11-23
  • 2021-12-29
  • 2021-05-26
  • 2021-12-06
  • 2022-12-23
  • 2021-05-14
相关资源
相似解决方案