


I would like to ask everyone for a detailed explanation of the recursive algorithm in java.
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 ofn 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The Steam Cloud error can be caused by many reasons. To play a game smoothly, you need to take some measures to remove this error before you launch the game. php.cn Software introduces some best ways as well as more useful information in this post.

You may see the “A connection to the Windows Metadata and Internet Services (WMIS) could not be established.” error on Event Viewer. This post from php.cn introduces how to remove the Windows Metadata and Internet Services problem.

KB5035942 update issues - crashing system commonly happens to users. Inflicted people hope to find a way out of the kind of trouble, such as crashing system, installation, or sound issues. Targeting these situations, this post published by php.cn wil

OneDrive is an online cloud storage service from Microsoft. At times, you might find OneDrive fail to upload photos to the cloud. If you are on the same boat, keep reading this post from php.cn Software to get effective solutions now!

If you have a program called PC App Store on your computer and did not purposely install it, then your PC may be infected with the malware. This post from php.cn introduces how to remove PC App Store malware.

Is Brothers: A Tale of Two Sons Remake not launching? Encountering Brothers: A Tale of Two Sons Remake black screen? Here this post on php.cn offers you tested solutions to assist you in addressing this problem.

Chris Titus Tech has a tool called Windows Utility that can help you easily create a debloated Windows 11/10 ISO to install a clean system. php.cn offers a full guide on how to do this thing using the Chris Titus tool.

Are you questioned about an issue that MSConfig keeps reverting to selective startup on your Windows? How to switch to normal startup if you require it? Try the methods explained in this php.cn post to find one that works for you.
