1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
public static final int MONTH = 15; public static void main(String[] args) { long f1 = 1L, f2 = 1L; long f; for(int i=3; i<MONTH; i++) { f = f2; f2 = f1 + f2; f1 = f; System.out.print("第" + i +"个月的兔子对数: "); System.out.println(" " + f2); } }
public static void main(String[] args) { for (int i=1;i<=20;i++){ int num = getNum(i); System.out.println("第 " + i + " 月份,兔子的数量为: " + num); } } public static int getNum(int i){ if((i==1)||(i==2)){ return 1; }else{ return getNum(i-1)+getNum(i-2); } }
|