Java实现兔子繁殖问题:斐波那契数列
{"title":"程序1"} java题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? \n{"程序1"}\njava\npublic class RabbitCount {\n public static void main(String[] args) {\n int month = 24; // 计算24个月的兔子总数\n int[] rabbitCount = new int[month]; // 存储每个月的兔子总数\n rabbitCount[0] = 1; // 第一个月有1对兔子\n rabbitCount[1] = 1; // 第二个月有1对兔子\n\n for (int i = 2; i < month; i++) {\n rabbitCount[i] = rabbitCount[i - 1] + rabbitCount[i - 2]; // 每个月的兔子总数等于前两个月的兔子总数之和\n }\n\n for (int i = 0; i < month; i++) {\n System.out.println("第" + (i + 1) + "个月的兔子总数为:" + rabbitCount[i]);\n }\n }\n}\n\n输出结果:\n\n第1个月的兔子总数为:1\n第2个月的兔子总数为:1\n第3个月的兔子总数为:2\n第4个月的兔子总数为:3\n第5个月的兔子总数为:5\n第6个月的兔子总数为:8\n第7个月的兔子总数为:13\n第8个月的兔子总数为:21\n第9个月的兔子总数为:34\n第10个月的兔子总数为:55\n第11个月的兔子总数为:89\n第12个月的兔子总数为:144\n第13个月的兔子总数为:233\n第14个月的兔子总数为:377\n第15个月的兔子总数为:610\n第16个月的兔子总数为:987\n第17个月的兔子总数为:1597\n第18个月的兔子总数为:2584\n第19个月的兔子总数为:4181\n第20个月的兔子总数为:6765\n第21个月的兔子总数为:10946\n第22个月的兔子总数为:17711\n第23个月的兔子总数为:28657\n第24个月的兔子总数为:46368\n\n每个月的兔子总数为斐波那契数列的第n项。
原文地址: https://www.cveoy.top/t/topic/pZ5M 著作权归作者所有。请勿转载和采集!