当前位置:网站首页>recursion: method calls itself
recursion: method calls itself
2022-08-01 23:37:00 【XLMN】
public class Study05 {
public static void main(String[] args) {/*** recursion: the method calls itself** /** *1. What is recursion?* * is the method itself calling itself.*** *2.f(int n);*** *f(int n){* * f(n-1);//Direct call;* *}*** *f(int n){* * a(n);//Indirect call;* *}*** *a(int n){* * f(n-1);* *}*** *3. Calculate 5!* *5!=5*4!* *4!=4*3!* *3!=3*2!* *2!=2*1!* *1!=1;*** *public static int factorial(int n){*** * int result=n*factorial(n-1);*** * return result;* *}*** *4. Two elements of recursion:* * 1) Rules for method invocation itself;* * 2) The end condition of the recursion; if there is no end condition, there will be a stack overflow, java.lang.StackOverflowError.* *5. Advantages and disadvantages of recursion:* * 1) The idea is simple;* * 2) Every time a method is called, the stack memory is opened up; the memory consumption is large, which affects the performance; if the performance requirements are high, avoid using recursion.* *6. Features of recursion:* * 1) A problem can be decomposed into several sub-problems with the same level;* * 2) The solution of the outer layer problem depends on the solution of the inner layer problem;* * 3) The solution to the problem of each layer is consistent;* * 4) There must be an end condition.* * 5) Recursive problems can be solved with loops.*/System.out.println(sum(4));}//Use recursive method to find factorialpublic static int sum(int a) {int temp = 0;if (a == 1) {temp = 1;//End condition} else {temp = a * sum(a - 1);}return temp;}}
边栏推荐
猜你喜欢

UML diagram of soft skills

Sql之各种Join
![[Camp Experience Post] 2022 Cybersecurity Summer Camp](/img/1e/716bafc679dc67d3d54bcc21a3b670.png)
[Camp Experience Post] 2022 Cybersecurity Summer Camp
![[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环](/img/63/16de443caf28644d79dc6e6889e5dd.png)
[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环

E - Integer Sequence Fair

企业防护墙管理,有什么防火墙管理工具?

还在纠结报表工具的选型么?来看看这个

cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed

伸展树的特性及实现

数据机构---第五章树与二叉树---二叉树的概念---应用题
随机推荐
YOLO等目标检测模型的非极大值抑制NMS和评价指标(Acc, Precision, Recall, AP, mAP, RoI)、YOLOv5中[email protected]与
npm npm
Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions
How to use pywinauto and pyautogui to link the anime lady and sister please go home
Chapter 11 Working with Dates and Times
欧拉路径与欧拉回路
月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~
20220725资料更新
Making a Simple 3D Renderer
一款简洁的文件传输工具
Flink学习第四天——完成第一个Flink 流批一体案例
6134. Find the closest node to the given two nodes - force double hundred code
Check if point is inside rectangle
Special characters & escapes in bat
D - Linear Probing- 并查集
When solving yolov5 training: "AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train"
CAKE:一个用于多视图知识图谱补全的可扩展性常识感知框架
chrome复制一张图片的base64数据
Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
Oracle database is set to read-only and read-write