Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the example of ava Recursive algorithm

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report