当前位置:网站首页>递归:方法调用自身
递归:方法调用自身
2022-08-01 23:33:00 【XLMN】
public class Study05 {
public static void main(String[] args) {
/**
* 递归:方法调用自身
*
* /*
* *1.什么是递归?
* * 就是方法自身调用自身。
* *
* *2.f(int n);
* *
* *f(int n){
* * f(n-1);//直接调用;
* *}
* *
* *f(int n){
* * a(n);//间接调用;
* *}
* *
* *a(int n){
* * f(n-1);
* *}
* *
* *3.计算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.递归的两个要素:
* * 1)方法调用自身的规则;
* * 2)递归的结束条件;如果没有结束条件,会出现栈溢出,java.lang.StackOverflowError。
* *5.递归的优缺点:
* * 1)思路简单;
* * 2)每调用一次方法,就开辟一次栈内存;内存消耗大,影响性能;如果对性能要求高,就避免使用递归。
* *6.递归的特点:
* * 1)一个问题可以分解成若干层相同的子问题;
* * 2)外层问题的解决依赖于内层问题的解决;
* * 3)每层问题的解决方法时一致的;
* * 4)要有结束条件。
* * 5)递归的问题都能用循环解决。
*/
System.out.println(sum(4));
}
//使用递归的方法求阶乘
public static int sum(int a) {
int temp = 0;
if (a == 1) {
temp = 1;//结束条件
} else {
temp = a * sum(a - 1);
}
return temp;
}
}
边栏推荐
- Access the selected node in the console
- C语言——分支语句和循环语句
- ELK日志采集
- Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions
- cdh6打开oozieWeb页面,Oozie web console is disabled.
- 高效工作文档产出归类
- [C language advanced] file operation (2)
- excel vertical to horizontal
- IDEA common plugins
- 程序员如何优雅地解决线上问题?
猜你喜欢
数据增强--学习笔记(图像类,cnn)
[C language advanced] file operation (2)
云原生DevOps环境搭建
cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed
【C语言进阶】文件操作(二)
CDH6的Hue打开出现‘ascii‘ codec can‘t encode characters
6134. Find the closest node to the given two nodes - force double hundred code
还在纠结报表工具的选型么?来看看这个
在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
Deep Learning Fundamentals - Numpy-based Recurrent Neural Network (RNN) implementation and backpropagation training
随机推荐
【C语言进阶】文件操作(二)
When solving yolov5 training: "AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train"
6133. 分组的最大数量
Convert LocalDateTime to Date type
Chapter 19 Tips and Traps: Common Goofs for Novices
CF1705D Mark and Lightbulbs
C语言——分支语句和循环语句
最短路模板
LocalDateTime转为Date类型
excel change cell size
从0到100:招生报名小程序开发笔记
数据机构---第五章树与二叉树---二叉树的概念---应用题
部门项目源码分享
drf生成序列化类代码
sys_kill系统调用
计算两点之间的中点
How do programmers solve online problems gracefully?
用virtualenv和Virtualenvwrapper虚拟环境管理工具创建虚拟环境
How to use pywinauto and pyautogui to link the anime lady and sister please go home
ELK日志采集