当前位置:网站首页>Recursive structure
Recursive structure
2022-07-04 03:18:00 【hv102】
Recursive structure consists of two parts :
- Define recursive headers . solve : When not to call your own methods . If there is no head , Will fall into a dead cycle , That is, the end condition of recursion .
- Recursive body . solve : When do I need to call my own methods .
- Example : Use recursion to find n
public class Test22 { public static void main(String[ ] args) { long d1 = System.currentTimeMillis(); factorial(10); long d2 = System.currentTimeMillis(); System.out.printf(" Recursive time-consuming :"+(d2-d1)); // Time consuming :32ms } /** The way to find factorial */ static long factorial(int n){ if(n==1){// Recursive header return 1; }else{// Recursive body return n*factorial(n-1);//n! = n * (n-1)! } } }The execution result is shown in the figure :


The flaw of recursion
Simple algorithm is one of the advantages of recursion . But recursive calls take up a lot of system stack , Memory consumption , When there are many levels of recursive calls, the speed is much slower than the loop , So be careful when using recursion .
边栏推荐
- Unity knapsack system (code to center and exchange items)
- Zblog collection plug-in does not need authorization to stay away from the cracked version of zblog
- Redis notes (I) Linux installation process of redis
- Key knowledge of C language
- Latex tips slash \backslash
- 長文綜述:大腦中的熵、自由能、對稱性和動力學
- This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
- PHP database connection succeeded, but data cannot be inserted
- Node write API
- MySQL data query optimization -- data structure of index
猜你喜欢

Jenkins continuous integration environment construction V (Jenkins common construction triggers)

Redis transaction

Node write API

1day vulnerability pushback skills practice (3)

This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t

長文綜述:大腦中的熵、自由能、對稱性和動力學

Network communication basic kit -- IPv4 socket structure

Jenkins configures IP address access

Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website

Contest3145 - the 37th game of 2021 freshman individual training match_ 1: Origami
随机推荐
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
Johnson–Lindenstrauss Lemma
Global and Chinese market of box seals 2022-2028: Research Report on technology, participants, trends, market size and share
PHP database connection succeeded, but data cannot be inserted
1day vulnerability pushback skills practice (3)
PMP 考試常見工具與技術點總結
Add IDM to Google browser
Setting methods, usage methods and common usage scenarios of environment variables in postman
96% of the collected traffic is prevented by bubble mart of cloud hosting
I stepped on a foundation pit today
Kiss number + close contact problem
Baijia forum the founding of the Eastern Han Dynasty
The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"
PID of sunflower classic
Global and Chinese markets for electroencephalogram (EEG) devices 2022-2028: Research Report on technology, participants, trends, market size and share
Keepalived set the master not to recapture the VIP after fault recovery (it is invalid to solve nopreempt)
Development of digital collection trading platform development of digital collection platform
Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi
Stm32bug [the project references devices, files or libraries that are not installed appear in keilmdk]
Short math guide for latex by Michael downs

