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

    技术2022-07-11  105

    package com.ujiuye.java; /*古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子, * 小兔子长到第三个月后每个月又生一对兔子, * 假如兔子都不死,问每个月的兔子对数为多少? * 分析: * 月份:1 2 3 4 5 6 7 8 9... * 对数 :1 1 2 3 5 8 13 21... */ public class Demo01 { public static void main(String[] args) { int month = 12; for (int i = 1; i <= month; i++) { int num = rabbitNum(i); System.out.println("第" + i + "个月兔子的总对数为:" + num + "对"); } } public static int rabbitNum(int month) { if (month == 1 || month == 2) { return 1; } else { return rabbitNum(month - 1) + rabbitNum(month - 2); } }

     

    Processed: 0.012, SQL: 12