Home  >  Article  >  Computer Tutorials  >  Not familiar with Java recursive method code

Not familiar with Java recursive method code

WBOY
WBOYforward
2024-01-13 19:48:19423browse

Not familiar with Java recursive method code

java code does not understand recursive methods

This is also a loop method. It may be difficult for beginners to understand... Let me explain

For example, the parameter in the fun() method is 100. Let me change it to 2.

The purpose of this recursive method is accumulation. The result is the same as the loop accumulation, but the execution method is different.

The process of program execution is as follows:

When you pass 2 in, the program will execute the content in else

That is return temp fun(temp-1);

The actual return is: 2 fun(2-1);

That is: 2 fun(1) changes the original parameter 3 to 2;

Let me start. This is an accumulation program, so take out 3 and assign it to the sum defined before, so the current sum=2;

Because this is a recursive method, the fun(int temp) method needs to be executed repeatedly;

It’s just that the parameter now becomes 1

So the next step will be like this:

Because the parameter is 1, it will enter the if

So it will return 1;

So the current sum should be the previous sum plus the 1 returned by the current fun(1)

so.....now sum should be: 2 1=3;

I just cited the cycle between two numbers. The cycle between the other 98 numbers is also like this

In short, recursion means to keep calling yourself until the conditions cannot be met, then it will not call itself.

Take a look, it may be a bit much. I want to explain it more clearly. I have encountered it before, but I have never asked anyone else. If you don’t understand or don’t understand, ask me again...

Java Recursive Algorithm If you have high income, please come in! Use recursion to implement

The work is very beautiful. If you can make an interface of this level, this small problem should not be difficult for you.

The question here is:

1. To design the structure of this graph, the easiest way is to use an array.

2. How to enumerate connected nodes. Simply put, it is a question of judging upper left, upper right, left, right, lower left, and lower right.

3. How to determine the same color in sequence? The simplest algorithm is the flooding method. Just start looking from the 6 directions above. After finding the next point, start looking from the 6 directions. . .

The approximate code is as follows:

class Rabbit

{

final int D_UP_LEFT = 1 ;

final int D_UP_RIGHT = 2;

final int D_LEFT = 3;

final int D_RIGHT = 4;

final int D_DOWN_LEFT = 5;

final int D_DOWN_RIGHT = 6;

getColor () ;

getCloseRibbit (int direction)

{

... // Here is the function to obtain adjacent rabbits in the specified direction

// If not return null .

}

int getColor () {}

List checkColor ()

{

ArrayList list = new ArrayList () ;

doCheckColor (list, this) ;

return list ;

}

void doCheckColor (List list, Rabbit r)

{

if (r.getColor () != this.getColor () || list.contains (r))

return ;

list.add (this) ;

for (int i = D_UP_LEFT ; i

{

Rabbit next = r.getCloseRibbit (i) ;

if (next != null)

next.doCheckColor (list, r) ;

}

}

}

The resulting list is the bunny of the same color you want.

How does JAVA use the recursive method to get nn from 1 to m

Recursion using arrays:

public class Test12 {

static int M = 4;

static int N = 3;

static int[] a= new int[]{1,2,3,4};

static int[] b = new int[N];

public static void main(String[] args){

C(M,N);

}

static void C(int m,int n){

int i,j;

for(i=n;i

b[n-1] = i-1;

if(n>1)

C(i-1,n-1);

else {

for(j=0;j

System.out.print(a[b[j]] " ");

System.out.println();

}

}

}

}

Output:

1 2 3

1 2 4

1 3 4

2 3 4

java implements recursive operation n! If n is not an integer, you will be prompted to re-enter

java implements recursive operation n! , enter n, if it is not an integer, you will be prompted to re-enter...

port java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class Test {

public int jiecheng(int num) {// Recursive factorial

if (num > 1)

return num * jiecheng(num - 1);

else if (num == 1)

return 1;

else

return 0;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in); // Get input from the keyboard

String num = """;

Pattern p = Pattern.compile("\\d "); // Regular expression, matching (1 to N digits) integer

Matcher m = null;

int k = 0;

while (true) {

System.out.print ("Please enter an integer:");

num = sc.nextLine(); // Get a line of input

m = p.matcher(num);

if (m.matches()) {

k = Integer.valueOf(num); // Convert string to integer

break;

} else

System.out.println ("Not an integer, please re-enter!");

System.out.println();

}

System.out.println(new Test().jiecheng(k)); //Call factorial method

}

}

Happy to live in a treasured land and prosper for thousands of years. May everything be prosperous for your family and your family. Happy to welcome the new year

The above is the detailed content of Not familiar with Java recursive method code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete