Title Description
There is a rabbit, from the 3rd month after birth a rabbit every month, the rabbit grew to the third month after the birth of a rabbit every month, if the rabbit is not dead, ask each month the total number of rabbits?
/**
* Count the total number of rabbits.
*
* @param monthcount months
* Total @return Rabbits
*/
public static int Gettotalcount (int monthcount)
{
return 0;
}
Input Description:
Enter int to indicate month
Output Description:
Output Rabbit Total int type
Input Example:
9
Output Example:
34
Fibonacci series (Fibonacci sequence), also known as the Golden Section series, because the mathematician Leonardo's Fibonacci (Leonardoda Fibonacci) to the rabbit breeding as an example of the introduction, so called "Rabbit series", refers to such a series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 、...... Mathematically, the Fibonacci sequence is defined as a recursive method: F (0) =0,f (1) =1,f (n) =f (n-1) +f (n-2) (n≥2,n∈n*).Subject :The number of rabbits in the nth month includes the number of last month plus the number of rabbits born in 3 months.the recursive type is:f (n) =f (n-1) +f (n-2) (n>=4)
<span style= "FONT-SIZE:18PX;" >import java.util.*;p ublic class main{public static void Main (string[] args) {Scanner scan=new Scanner (system.in); while (Scan.hasnext ()) {int month=scan.nextint (); System.out.println (Sum (month));}} static int Sum (int month) {int sum=0;if (month==1) {sum=1;} else if (month==2) {sum=1;} else if (month==3) {sum=2;} Else{sum=sum (month-1) +sum (month-2);} return sum;}} </span>
Huawei oj--counts the total number of rabbits per month