Java经典编程题50道之一

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

public class Example01 {
    public static void main(String[] args) {
        int a = 8;
        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;
    }
}

posted @ 2017-05-31 15:34  本宫在,尔等都是妃  Views(315)  Comments(0Edit  收藏  举报