免子生免子
public class J01{ //用整形数组实现 private static int[] calRubbit(int mouth) { int[] Rubbits = new int[mouth]; for(int i=0;i<mouth;i++) { if(i<2) Rubbits[i]=1; else Rubbits[i]=Rubbits[i-1]+Rubbits[i-2]; } return Rubbits; } public static void main(String args[]){ int mouth = 20; int[] Rubbits = calRubbit(mouth); for(int i : Rubbits) System.out.print(i+"\t"); } }
public class J01o1{ //用变量之间的赋值来解决 public static void main(String args[]) { System.out.println(1); System.out.println(1); int a1 = 1 , a2 = 1 ,a3=0,mouth=18; for(int i=0;i<mouth;i++) { a3 = a1 + a2; a1 = a2; a2 = a3; System.out.println(a3); } } }
public class J01o2{ //此处用递归求出某个月的兔子对数 public static int RubbitNums(int mouth) { if(mouth<3) return 1; else return RubbitNums(mouth-1)+RubbitNums(mouth-2); } public static void main(String argsp[]) { for(int i=0;i<20;i++) { System.out.println(RubbitNums(i+1)); } } }