In order to improve their ability to code and algorithms, I decided to learn a problem every day to absorb the ideas of predecessors.
"Program 1" Testrabbit.java
Title: Classical Questions: There are a pair of rabbits, from the first 3 months 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?
Program Analysis: The law of the number of rabbits for the series: 1,1,2,3,5,8,13 ... in fact, the Fibonacci sequence can be implemented using recursion.
1 /**2 * Rabbit Problem3 * 2016/5/94 * Fibonacci Sequence Evaluation5 * Title: Classical question: There is a pair of rabbits, starting from the 3rd month after birth a pair of rabbits every month,6 * Rabbits grow to the third month after a month and a pair of rabbits, if the rabbit is not dead, ask the total number of rabbits each month? 7 * Program Analysis: The law of Rabbits for the series 1,1,2,3,5,8,13,21 ....8 */9 Packageorg;Ten One ImportJava.util.Scanner; A Public classTestrabbit { - - Public Static voidMain (string[] args) { theScanner input =NewScanner (system.in); - intn =input.nextint (); - intnum =Fun (n); -System.out.println ("+n+" month of the total number of rabbits: "+num); + } - + Public Static intFunintN) { A if(n = = 1 | | n ==2){ at return1; -}Else{ - return(n-1) + Fun (n-2)); - } - - } in -}
Testrabbit.java
Java Basic algorithm problem