当前位置:网站首页>递归:方法调用自身
递归:方法调用自身
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;
}
}
边栏推荐
- 解决端口占用
- Solve the port to take up
- [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
- numpy.isclose
- D - Linear Probing- 并查集
- Sql之各种Join
- PDF转Word有那么难吗?做一个文件转换器,都解决了
- CDH6的Hue打开出现‘ascii‘ codec can‘t encode characters
- SQL Server (design database--stored procedure--trigger)
- cmd指令
猜你喜欢

cmd指令
[email protected]与YOLO等目标检测模型的非极大值抑制NMS和评价指标(Acc, Precision, Recall, AP, mAP, RoI)、YOLOv5中[email protected]与

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

C language - branch statement and loop statement

深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练

drf生成序列化类代码

Leetcode 129求根节点到叶节点数字之和、104二叉树的最大深度、8字符串转换整数(atoi)、82删除排序链表中的重复元素II、204二分查找、94二叉树的中序遍历、144二叉树的前序遍历

云原生DevOps环境搭建

伸展树的特性及实现

The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
随机推荐
excel vertical to horizontal
还在纠结报表工具的选型么?来看看这个
Calculate the angle of a line defined by two points
ICLR 2022最佳论文:基于对比消歧的偏标签学习
数据增强--学习笔记(图像类,cnn)
vscode hide menu bar
Background project Express-Mysql-Vue3-TS-Pinia page layout-sidebar menu
企业防护墙管理,有什么防火墙管理工具?
cdh6打开oozieWeb页面,Oozie web console is disabled.
Access the selected node in the console
Chapter 11 Working with Dates and Times
IDEA入门看这一篇就够了
CF1703G Good Key, Bad Key
怎样做才能让这条SQL变成一条危险的SQL?
论文理解【RL - Exp Replay】—— Experience Replay with Likelihood-free Importance Weights
Deep Learning Fundamentals - Numpy-based Recurrent Neural Network (RNN) implementation and backpropagation training
加载字体时避免隐藏文本
Calculate the distance between two points
PostgreSQL Basics--Common Commands
When solving yolov5 training: "AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train"