当前位置:网站首页>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;}
}
边栏推荐
- 企业防护墙管理,有什么防火墙管理工具?
- Chapter 12 End-User Task As Shell Scripts
- UI自动化测试框架搭建-标记性能较差用例
- Interpretation of the paper (GSAT) "Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism"
- CDH6的Hue打开出现‘ascii‘ codec can‘t encode characters
- numpy.hstack
- Flink学习第五天——Flink可视化控制台依赖配置和界面介绍
- cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed
- perspectiveTransform warpPerspective getPerspectiveTransform findHomography
- Check if point is inside rectangle
猜你喜欢
仿牛客网项目第三章:开发社区核心功能(详细步骤和思路)
Is TCP reliable?Why?
chrome copies the base64 data of an image
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D Solution
Quartus uses tcl files to quickly configure pins
Background project Express-Mysql-Vue3-TS-Pinia page layout-sidebar menu
C language - branch statement and loop statement
cdh6打开oozieWeb页面,Oozie web console is disabled.
From 0 to 100: Notes on the Development of Enrollment Registration Mini Programs
Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights
随机推荐
企业防护墙管理,有什么防火墙管理工具?
经典文献阅读之--DLO
Chapter 11 Working with Dates and Times
numpy.unique
Additional Features for Scripting
How to use pywinauto and pyautogui to link the anime lady and sister please go home
如何更好的理解的和做好工作?
PostgreSQL 基础--常用命令
Flink学习第四天——完成第一个Flink 流批一体案例
Getting started with IDEA is enough to read this article
sys_kill系统调用
访问控制台中的选定节点
测试岗月薪5-9k,如何实现涨薪到25k?
TCP 可靠吗?为什么?
[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环
Work for 5 years, test case design is bad?To look at the big case design summary
切面打印调取的方法
GIF制作-灰常简单的一键动图工具
CAKE:一个用于多视图知识图谱补全的可扩展性常识感知框架
研发团队数字化转型实践