斐波那契数列兔子繁殖问题:3月到12月兔子数量计算

题目描述:

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

运行结果:

1:The number of rabbits is:1 2:The number of rabbits is:1 3:The number of rabbits is:2 4:The number of rabbits is:3 5:The number of rabbits is:5 ...

输入格式:

输出格式:

字符串

输入样例:

输出样例:

1:The number of rabbits is:1 2:The number of rabbits is:1 3:The number of rabbits is:2 4:The number of rabbits is:3 5:The number of rabbits is:5 6:The number of rabbits is:8 7:The number of rabbits is:13 8:The number of rabbits is:21 9:The number of rabbits is:34 10:The number of rabbits is:55 11:The number of rabbits is:89 12:The number of rabbits is:144

程序模板

___________________ Main{

	public static void main(________________________) {

		for (__________________; i <= ________________; i++) {

			System.out.println(i + ':The number of rabbits is:' + getRabbitNum(i));

		}

	}

	public ___________________    ________ getRabbitNum(________________________) {

		if (n == 1 || n == 2)

			return 1;

		else

			return   _____________________________________ 

	}

}

代码实现:

public class Main{

    public static void main(String[] args) {
        for (int i = 3; i <= 12; i++) {
            System.out.println(i + ':The number of rabbits is:' + getRabbitNum(i));
        }
    }

    public static int getRabbitNum(int n) {
        if (n == 1 || n == 2)
            return 1;
        else
            return getRabbitNum(n-1) + getRabbitNum(n-2);
    }
}
斐波那契数列兔子繁殖问题:3月到12月兔子数量计算

原文地址: https://www.cveoy.top/t/topic/pcmw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录