Java Classic problem--Rabbit

Source: Internet
Author: User
Title: Classical question: There are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits every month, small rabbit long to the third month after the birth of a pair of rabbits each month, if the rabbit is not dead, ask the number of rabbits each month.

1. Procedure analysis: Rabbit's rule for series 1,1,2,3,5,8,13,21 .... (not sure if this is the subject of the offer)


When I see this problem, if you do not see the back of the "Rabbit Rule for series 1,1,2,3,5,8,13,21", I think so, the rabbit is a birth of two, and the total is not the right, is the number, do not know the topic to express the same as I began to think, or with " The law of rabbits is the same as the 1,1,2,3,5,8,13,21 "information provided by the series."


If according to the "Rabbit Law for the series 1,1,2,3,5,8,13,21" information provided to analyze:

F (1) =1

F (2) =1

F (3) =f (2) +f (1) *1=2//f (1) * 1: The rabbit for the first month can produce a pair of each pair.

F (4) =f (3) +f (2) *1//f (2) * 1: The rabbit for the second month can produce a pair of each pair.

......

By analogy, we can conclude that

F (n) =f (n-1) +f (n-2) *1//n>=3

Java code:

public class Practise1 {
	
	static int MONTH = 6;//months
	static int TYPE = 1;  A 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;
		}
		if (month = = month) {
			System.out.println ("month=" +month+ "sum=" +sum);
		}
		return sum;
	}
	
	
	
	public static void Main (string[] args) {
		
		(New Practise1 ()). GetCount (MONTH, TYPE);
		
	}




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.