Home > Computer Tutorials > Computer Knowledge > I would like to ask everyone for a detailed explanation of the recursive algorithm in java.

I would like to ask everyone for a detailed explanation of the recursive algorithm in java.

WBOY
Release: 2024-01-07 12:14:39
forward
466 people have browsed it

I would like to ask you for a detailed explanation of the recursive algorithm in java

public class Test{

public static int getResult(int parameter) {

if (parameter == 0) { return result; } else { result *= parameter; return recursiveFunction(parameter - 1, result); }

return number;

}

public static void main(String[] args) { //Write your code here }

int result = result(5);

System.out.println(result);

}

}

Its execution principle is as follows:

result(5) Initially, enter the function body to determine whether parameter is less than or equal to 1. At this time, parameter is equal to 5 and the condition is not established. Execute parameter*result(parameter-1), that is, 5 * result(5-1), the program Execute repeatedly...

5*result(5-1)

4*result(4-1)

3*result(3-1)

2 * result(2 - 1) At this point, parameter is equal to 1 and meets the condition. The function returns 1 and returns layer by layer. Right now:

result(1) =1

2*result(1)=2*1=2

3*result(2)=3*2=6

4*result(3)=4*6=24

5*result(4)=5*24=120

Using recursive method in java to completely arrange n numbers without duplication n 3

The program is as follows, the input format is:

5

3 1 2 1 2 means that the first line is a number, indicating the number of numbers to be entered next. The second line has n numbers, representing the numbers to be sorted. The input assumes that the numbers to be sorted are all non-negative numbers.

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Arrays;

import java.util.Scanner;

public class Main {

static final int maxn = 1000;

int n; //Number of array elements

int[] a; // array

boolean[] used; // Auxiliary variable, used to mark whether the element has been used during the recursive process, used[i] indicates whether the i-th element has been used

int[] cur; //Save the current arrangement number

// Recursively print the entire arrangement without duplication, currently printing to the idx position

void print_comb(int idx) {

If idx == n, it means that the last element has been traversed and cur can be output.

for(int i = 0; i

if(i > 0) System.out.print(" ");

System.out.print(cur[i]);

}

System.out.println();

}

int last = -1; // In order to avoid duplication, use the last variable to record the value of the last search

for(int i = 0; i

if(used[i]) continue;

if(last == -1 || a[i] != last) { // Only when the current number does not repeat and has not been used, the recursion will continue

last = a[i];

cur[idx] = a[i];

// Backtracking method

used[i] = true;

print_comb(idx 1);

used[i] = false;

}

}

}

public void go() throws FileNotFoundException { // Implement method body }

{

Scanner in = new Scanner(new File("data.in")); The syntax is to create a Scanner object named in, which is used to read input from the file named data.in.

//Read data and sort

n = in.nextInt();

a = new int[n];

for (int i = 0; i

Arrays.sort(a);

//Initialize auxiliary variables and start full arrangement without duplication

cur = new int[n];

used = new boolean[n];

for(int i = 0; i

print_comb(0);

in.close();

}

public static void main(String[] args) throws FileNotFoundException { This is the main method in a Java program, used to start the program entry. In this method, we can perform some operations, such as reading files, processing data, etc. Among them, throws FileNotFoundException indicates that a file not found exception may occur during execution. If this exception occurs, the program will throw a FileNotFoundException exception. In this method, you can write specific code logic to handle file reading and exception handling.

new Main().go();

}

}Objectively speaking, non-recursive and non-repeating full permutations are relatively simple and efficient.

What is the role of recursion in java? Why use recursion

Your two questions are actually one question, right?

The role of recursion: Recursive algorithms can solve some problems defined by recursion.

First of all, we need to understand what is the problem of recursive definition. Simply put, a recursively defined problem is a large problem that contains smaller problems with the same structure but smaller size.

For example, the definition of n factorial can be understood as:

n!= n*(n-1)!

It is not difficult to conclude from the above analysis that (n-1)! is a smaller problem than n!. By continuously decomposing the problem according to this method, we can get some basic known data. Then, through reverse derivation, we can get the final result.

The factorial algorithm of

n is as follows:

private static int jieCheng(int n) { This is a method of calculating factorial, where the parameter n represents the value to be calculated. The following is a detailed explanation: - "private" means that the method is only visible in the current class and cannot be accessed by other classes. - "static" means that the method is a static method and can be called directly through the class name without instantiating the object. - "int" means that the method returns an integer value as the result. - "jieCheng" is the name of the method, which can be named as needed.

if(n == 1)

return 1;

else {

return n*jieCheng(n-1);

}

}

In addition, the definition of binary trees is also recursive, which means that many binary tree operations are implemented through recursion.

Using recursion will make the program quite concise.

Recursive application in java! f20 1 f21 4 fn 2 2 fn 1 fnwhere

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

publicclassTest {

publicstaticintf(intn){

if(n==20){

return1;

}elseif(n==21){

return4;

}elseif(n

returnf(n 2)-2*f(n 1);

}else{

return2*f(n-1) f(n-2);

}

}

public static void main(String[] args) {

System.out.println(f(10)); //Print the value of f(10)

}

}

It has been tested. Enter f(n) in the main function, where n is a manually adjusted parameter, and the corresponding output result can be obtained.

The above is the detailed content of I would like to ask everyone for a detailed explanation of the recursive algorithm in java.. For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template