Java language programming (fourteen) application examples of recursive algorithms, and the copy, transfer, and return of arrays

 1. Array copy

      In a program, it is often necessary to copy an array or part of an array. In this case, our first thought may be to try to use an assignment statement: list1=list2; but this statement does not copy the contents of the array referenced by list1 to list2 , And just copy the reference value of list1 to list2. After this statement, both list1 and list2 point to the same array, and the array originally referenced by list2 will be automatically retracted.

      In Java, you can use assignment statements to copy variables of basic data types, but you cannot copy arrays. Assigning an array variable to another array variable is actually a reference copy, so that both variables point to the same memory address.

      There are three ways to copy an array:

    (1) Use the loop statement to copy the elements of the array one by one.

    (2) Use the static method arraycopy in the System class.

    (3) Use the clone method to copy, which we will introduce in a later article.

      We can use a loop to copy each element in the source array to the corresponding element in the target array. For example, use a for loop to copy sourceArray to targetArray:

      int[]sourceArray ={2,1,5,4,8};

      int[]targetArray = new int[sourceArray.length];

          for(int i=0;i<sourceArray.length;i++){

              targetArray[i] = sourceArray[i];

}

      Another way is to use the arraycopy method of the java.lang.System class to copy the array instead of using loops. The syntax of arrarcopy is as follows:

      arraycopy(sourceArray,src_pos,targetArray,tar_pos,length);

      Among them, the parameters src_pos and tar_pos represent the starting position in the source array sourceArray and the target array targetArray respectively. The number of copied elements is specified by the parameter length. Let's take an example:

      System.arraycopy(sourceArray,0,targetArray,0,sourceArray.length);

      2. Pass an array to the method

      Just as the basic data is passed to the method before, an array can also be passed to the method. We can call the printArray method by passing an array to display it.

      3. Return the array from the method

      We can pass an array to the method when calling the method, and the method can also return an array. For example, the following method returns an array in the reverse order of the input array:

      public static int[] reverse(int[] list){

          int[] result = new int[list.length];

          for(int i=0,j=result.length-1;i<list.length;i++,j--){

               result[j]=list[i];

               }

      return result;

}

      4. Recursive algorithm

       What is recursion? A procedure or function has a method of directly or indirectly calling itself in its definition or description. A recursive function is a function that directly or indirectly calls itself, that is, it calls itself. So when do we generally use recursion?

   Recursion is a commonly used programming technique. Its basic idea is to "call yourself". A method of using recursive technology is to call itself directly or indirectly. The recursive method actually embodies the idea of ​​"by analogy" and "repeat with the same steps". It can use simple programs to solve some complex calculation problems, but the amount of calculation is relatively large.
    There are also some data structures, such as binary trees, which are inherently recursive. In addition, there is a type of problem that does not have an obvious recursive structure, but it is easier to write programs with recursive programs than other methods, such as the Eight Queens Problem, the Tower of Hanoi .

    Because of the universality of recursive programs, we should learn to use recursion to solve problems. Both direct recursive program and indirect recursion must realize the parameter transfer when the current layer calls the next layer, and obtain the result returned by the next layer, and call the upper layer to return the result of the current layer. As for the saving and restoring of the scene in each layer call, it is automatically realized by the program without manual intervention. Therefore, in the design of the recursive program, the key is to find out the parameters required by the call, the returned result and the conditions for the end of the recursive call.

      Let's now take a Java application example to apply the recursive algorithm, such as the number of divisions of positive integers. For example, 3 can be decomposed into 3 2+1 and 1+1+1, 4 can be decomposed into 4 3+1, 2 +2, 2+1+1, 1+1+1+1, 6 can be decomposed into 6 5+1, 4+2, 4+1+1, 3+3, 3+2+1, 3+1+ 1+1, 2+2+2, 2+2+1+1, 2+1+1+1+1, 1+1+1+1+1+1, so a q(n, m) meaning is established To divide n positive integers, m is the limit, and the maximum number of divisions cannot be greater than m. We can establish the following recursive relationship:

      q(n,m)=1-------n=1,m=1

      q(n,m)=q(n,n)------n<m

      q(n,m)= 1+q(m,m-1)------n=m

      q(n,m)=q(n,m-1)+q(n-m,m)------n>m>1

      The program list is as follows:

package digui;

import java.util.Scanner;

/**
 *
 * @author john
 */
public class Digui {
 public static int q(int n, int m){
  if (m<0 || n<0){
   return 0;
  }
  else if (m == 1 || n == 1){
   return 1;
  }
  else if (n<m){
   return q(n, n);
  }
  else if (n == m){
   return 1 + q(n, n - 1);
  }
  else{
   return q(n, m - 1) + q(n - m, m);
  }
 }
 public static void main(String[] args) {
  System.out.print("Please input n\n");
  Scanner input=new Scanner(System.in);
  int n=input.nextInt();
  System.out.print("Please input m\n");
  int m=input.nextInt();
  int s = q(n, m);
  System.out.print(s);
 }

}

image

      As shown in the figure, if we enter 6 6 and the result is 11, the program is correct.


Guess you like

Origin blog.51cto.com/15064656/2602766