Title: Classical Questions: There are a pair of rabbits, from the 3rd month after birth a pair of rabbits each month, the rabbit long to the third month after the birth of a pair of rabbits, if the rabbit is not dead, ask each month the total number of rabbits?
1. Program Analysis: The law of Rabbits for the series 1,1,2,3,5,8,13,21 ....
Package Org.llh.demo01;import Java.util.arraylist;public class DemoTest002 {static arraylist<integer> list = new ArrayList (); int number;//count of rabbits after one months public static void Main (string[] args) { DemoTest002nd = new DemoTest002 (); St.fun (); St.out (); } private void Fun () {//The number of rabbits for 12 months is saved in the list linked list list.add (1); List.add (1); Rabbits in the first two months for (int i = 3;i<=12;i++) {Number = List.get (i-2) +list.get (i-3); List.add (number); } } private void Out () {//Output 12-month rabbit number int i =1; for (int j:list) { System.out.println ("+" + (i++) + "Rabbit number of months is" +j);}}}
12 months Total
Package Org.llh.demo01;public class DemoTest001 {static int month[] = {1,2,3,4,5,6,7,8,9,10,11,12};//months static in t TYPE = 1; Couple of pairs of private int getcount (int month, int type) { int sum = 0; if (month = = 1 | | month ==2) { sum = 1; } else{ sum = GetCount (month-1,1) + GetCount (month-2,1) *type; } return sum; } public static void Main (string[] args) { int count=0; for (int a:month) { int sum = (new DemoTest001 ()). GetCount (A, TYPE); Count = Sum+count; } System.out.println (count); } }
Java Classic Bunny problem