Java deduction of recursive function

Source: Internet
Author: User
Tags function definition
recursion | Function 1, recursive function definition:

A: A recursive function, called a function, calls itself directly or indirectly in the body of a function, that is, the function's nesting is the function itself.

2, recursion way: recursive call has direct recursive and indirect recursion two kinds of way.

A: Direct recursion: The call function itself appears in the function.

Example 1: The following code asks the Fibonacci number, the first and second items of the Fibonacci series are 1, and then each of the preceding items is the sum of the first two items, i.e. 1, 1, 2, 3, 5, 8, 13 ....

Program code:

public class Test {

public static void Main (String args[]) {

int x1 = 1;

int sum = 0;

int n = 7;



for (int i = 1; I <= n; i++) {

X1 = func (i);

sum = sum + x1;

}

System.out.println ("sum=" + sum);

}



public static int func (int x) {

if (x > 2)

Return (func (x-1) + func (x-2));

Else

return 1;

}

}



B: Indirect recursion: The other function is called in the function, and the other function has called this function.

Example 2: Calculates the Fibonacci sequence by using the indirect recursive return.

Program code:

public class Test {

public static void Main (String args[]) {

int x1 = 1;

int sum = 0;

int n = 7;



for (int i = 1; I <= n; i++) {

X1 = func1 (i);

sum = sum + x1;

}

System.out.println ("sum=" + sum);

}

public static int func1 (int a) {

int b;

B=func2 (a);

return b;

}



public static int Func2 (int b) {

if (b> 2)

Return (Func1 (b-1) + func1 (b-2));

Else

return 1;

}

}

3. Why should I use a recursive function? What are the drawbacks of recursive functions?

A: The purpose of recursion is to simplify programming and make the program easy to read.

Example 3: The following recursive function is not used to compute the Fibonacci sequence.

Program code:

public class Test {

public static void Main (String args[]) {

int n=7;

int a=1, b=1, temp;

int sum=2;



for (int i=3; i<=n; i++) {

Temp=a+b; A=b; B=temp;

Sum=sum+temp;

}

System.out.println ("sum=" + sum);

}

}

From the above example we can find that although the non recursive function is efficient, but more difficult to program, the readability is poor. The disadvantage of a recursive function is that it increases the overhead of the system, which means that the stack memory takes up a bit more each time it is recursively.



4, the condition of the recursion:

A: You need to have the statement to complete the task to meet the recursive requirements (reduce rather than diverge).

5, the recursive Advanced step:

Example 4:

Solver: If a heifer starts a cow every year from the fourth year of birth, how many cows are there in the nth years?



Program code:

public class Test3 {

public static void Main (String args[]) {

int n=10; Number of years to view

SYSTEM.OUT.PRINTLN ("Total" +cattle (n) + "head heifer!") ");

}

public static int cattle (int n) {

if (n<=0)

return 0;

if (n<=3)

return 1;

Return cattle (n-1) + cattle (n-3);//Here is recursion to understand well.

}

}



Rule: The recursive function of such a problem is:

If the requirement is fourth years from birth, then the recursive function is cattle (n-1) + cattle (n-3),

If the requirement is fifth years from birth, then the recursive function is cattle (n-1) + cattle (n-4),

。。。。

by analogy.

(The original code all under Jbuilderx debugging Pass)



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.