Java Recursive algorithm classic example (rabbit problem, factorial, 1 to 100 accumulation) __java recursive algorithm classic instance rabbit problem factorial 1 to

Source: Internet
Author: User

Title: Classical Questions: 3 months from the month of a pair of rabbits, the 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.

Analysis: First of all, we need to understand the meaning of the topic refers to the total logarithm of the rabbit every month; if the rabbit is divided into small and middle three species, rabbits will produce a pair of rabbits every month after three months of birth,

Then we assume that the first month of the rabbit as a small rabbit, the second month for the Rabbit, the third month after the Big Rabbit, then the first month, there are 1, 0, 0, the second month are 0, 1, 0,

The third month were 1, 0, 1, fourth month respectively, 1, 1, 1, fifth month respectively 2, 1, 2, sixth month respectively 3, 2, 3, seventh month respectively 5, 3, 5 ...

The total number of rabbits were: 1, 1, 2, 3, 5, 8, 13 ...

So we have a rule that from the third month onwards, the total number of rabbits behind is equal to the total number of rabbits in the preceding two months, which is the Fibonacci sequence.


public class test{public
	static void Main (string[] args) {
		int i = 1;
		for (i=1;i<=20;i++) {
			System.out.println ("Rabbit" +i+ "The total number of months is:" +f (i));
		}
	public static int f (int x) {
		if (x==1 | | x==2) {return
			1;
		} else{return
			F (x-1) +f (x-2);}}


Add from 1 to 100:

public class Digui {public
	int sum (int i) {
	   if (i==1) {return
			1;
	   }
	   Return I+sum (i-1);
	}  
	public static void Main (string[] args) { 
	   Digui test=new Digui ();  
	   SYSTEM.OUT.PRINTLN ("Calculated result:" +test.sum () + "!");  
	}  

From 1 to 100 factorial:

It should be noted that the computed results are too large for the program to return, and typically return 0. Then the int, long is not satisfied, so use the BigInteger

public class Digui {public
	BigInteger sum (int i) {
		if (i = = 1) {return
			biginteger.one;
		}
		return biginteger.valueof (i). Multiply (sum (i-1));
	}

	public static void Main (string[] args) {
		Digui test = new Digui ();
		try {
			System.out.println ("Calculated result:" + test.sum + "!");
		catch (Exception e) {
			//TODO auto-generated CATC H Block
			e.printstacktrace ();}}


In addition to remind the real project to use the recursive algorithm carefully, roughly summed up the advantages and disadvantages of the recursive algorithm:

Advantages:

The code is simpler and clearer, and the readability is better

Disadvantages:

Because recursion requires a system stack, space consumption is much larger than non recursive code. Moreover, if the recursion depth is too large, the system may not hold up.



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.