当前位置:网站首页>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
![[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph](/img/63/16de443caf28644d79dc6e6889e5dd.png)
[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph

月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~

一款简洁的文件传输工具

System availability: 3 9s, 4 9s in SRE's mouth... What is it?
Background project Express-Mysql-Vue3-TS-Pinia page layout-sidebar menu

程序员如何优雅地解决线上问题?

GIF制作-灰常简单的一键动图工具

Secondary Vocational Network Security Competition B7 Competition Deployment Process

深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
随机推荐
npm npm
企业防护墙管理,有什么防火墙管理工具?
6134. 找到离给定两个节点最近的节点-力扣双百代码
Oracle database is set to read-only and read-write
程序员如何优雅地解决线上问题?
YOLO等目标检测模型的非极大值抑制NMS和评价指标(Acc, Precision, Recall, AP, mAP, RoI)、YOLOv5中[email protected]与
简单3D渲染器的制作
What can be done to make this SQL into a dangerous SQL?
Leetcode 129求根节点到叶节点数字之和、104二叉树的最大深度、8字符串转换整数(atoi)、82删除排序链表中的重复元素II、204二分查找、94二叉树的中序遍历、144二叉树的前序遍历
对于在新标签页中打开的链接,始终使用“noopener”或“noreferrer”
The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
【参营经历贴】2022网安夏令营
怎样做才能让这条SQL变成一条危险的SQL?
numpy.around
请问什么是 CICD
月薪12K,蝶变向新,勇往直前—她通过转行测试实现月薪翻倍~
[Recommended books] The first self-driving technology book
Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions
Work for 5 years, test case design is bad?To look at the big case design summary
Convert LocalDateTime to Date type