Java programming-the number of rabbits in a year
Question: classical question: there is a rabbit. From the first 3rd months after birth, a rabbit is born every month. After the third month, a rabbit is born every month. If the Rabbit does not die, what is the total number of rabbits per month?
1. program analysis: the rabbit rule is a sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21 ....
The typical polasche problem is not mentioned much, and the code is ready directly.
Import java. util. arrayList; public class SecondThread {static ArrayList <Integer> list = new ArrayList (); int number; // calculate the number of rabbits in the next month. public static void main (String [] args) {SecondThread st = new SecondThread (); st. fun (); st. out ();} private void fun () {// Save the number of rabbits for 12 months into the list set. add (1); list. add (1); // number of 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 the rabbit count for 12 months int I = 1; for (int j: list) {System. out. println ("day" + (I ++) + "the number of rabbits in the month is" + j );}}}