Huawei oj--counts the total number of rabbits per month

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.