【程序1】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
package test; public class test { public static int recursion(int month) { if(month <= 2) return 2; else return recursion(month-1)+recursion(month-2); } public static void main(String[] args) { long a = System.currentTimeMillis(); for(int i = 1; i<=15; i++) System.out.println("The number of rabbits in the "+i+" month is "+recursion(i)); System.out.println(System.currentTimeMillis() - a); } }