Java interpretation of recursive functions (original)

xiaoxiao2021-03-06  105

Java interpretation of recursive functions

lxgljj

1. Definition of the recursive function:

A: The recursive function is called the self-procurement function, directly or indirectly in the function, that is, the nested function of the function itself.

2, recursive way: recursive calls have direct recursive and indirect delivery.

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

Example 1: The following code refers to the number N item, the first and second items of the Fiboaccai number, the first and second items, and each of the top two, namely 1, 1, 2, 3, 5 8, 13. . . .

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 (X - 1) FUNC (X - 2));

Else

Return 1;

}

}

B: Indirect recursive: other functions are called in the function, and the other functions are called this function.

Example 2: Use indirect return to calculate the above-mentioned Fiboacci number.

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 is the disadvantage of recursive functions?

A: The purpose of recursive is to simplify the design and make the program easy to read.

Example 3: The following does not use the recursive function to continue to calculate the above-mentioned Fiboacci number.

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, it is difficult to program, and readability is poor. The disadvantage of recursive functions is to increase system overhead, that is, every time it is recursive, the stack memory will take more than one. 4, recursive conditions:

A: You need to have a statement to complete the task, you need to meet the recursive requirements (decrease rather than divergence).

5, recursive advancement:

Example 4:

Programming Solving: If a small cow, from birth, every year, every year, every year, how many cows are there in the nine?

code:

Public class test3 {

Public static void main (string args []) {

INT n = 10; // to view the number of years

System.out.println ("Again" Cattle (N) "Head Cow!");

}

Public static int Cattle (int N) {

IF (n <= 0)

Return 0;

IF (n <= 3)

Return 1;

Return Cattle (N-1) Cattle (N-3); // This is a recursive to understand.

}

}

Law: The recursive function of such issues is:

If required, the recursive function is Cattle (N-3) from birth from birth to the fourth year.

If required, the recursive function is Cattle (N-4) from birth from birth to the fifth year.

. . . .

And so on.

(The original code is all debugged under JBuilderx)

转载请注明原文地址:https://www.9cbs.com/read-103338.html

New Post(0)