How to understand the example of ava Recursive algorithm
How to understand the example of ava recursive algorithm, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Three elements of recursion:
1. Clear recursive termination conditions
2. Give the treatment of recursive termination.
3. Extract the logic of repetition and reduce the scale of the problem.
1, 1, 2, 3, 3, 3, 2, 2, 3, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + n
Import java.util.Scanner; public class Recursion {public static void main (String [] args) {Scanner in = new Scanner (System.in); int n = in.nextInt (); System.out.println (sum (n));} public static int sum (int n) {if (n = = 1) {return n;} else {return n + sum (NMAT 1);}
2. 1 * 2 * 3 *... * n
Import java.util.Scanner; public class Recursion {public static void main (String [] args) {Scanner in = new Scanner (System.in); int n = in.nextInt (); System.out.println (multiply (n));} public static int multiply (int n) {if (n = = 1) {return n;} else {return n*multiply (NMu1);}
3. Fibonacci series
The first two items are both 1, and the third item begins, and each item is equal to the sum of the first two items. That is: 1, 1, 1, 1, 2, 3, 5, 8, …
Import java.util.Scanner; public class Recursion {public static void main (String [] args) {Scanner in = new Scanner (System.in); int n = in.nextInt (); System.out.println (fun (n));} public static int fun (int n) {if (n)