Recursive
Recursion is a function that invokes itself in its function body. Executing a recursive function calls itself repeatedly, entering a new layer each time it is called.
The recursive function must have an end condition .
Recursion is divided into two stages of the push-back and recursion, and when it is pushed back, until the wall is encountered, the wall is the end condition.
So recursion has to have two elements, the back-push wall and the recursive relationship
examplescalculates the factorial of n#include <stdio.h>
int factorial (int n) {int result; if (n<0)//judgment Exception {printf ("Input error!\n"); return 0; } else if (n==0 | | n==1) {result = 1; Push back wall} else {result = factorial (n-1) * n; The relationship between this number and the previous number is a recursive relationship. } return result; int main () {int n = 5; Enter the number 5, calculate the factorial of 5 printf ("Factorial =%d of%d", N,factorial (n)); return 0;}
The program calculates the factorial of 5, executes the recursion first, returns 1 when N=1 or n=0, and then pushes back to calculate and return. From this you can see that the recursive function must have an end condition .
Recursive function Features:
1. Each level function call has its own variable, but the function code is not copied, such as calculating the factorial of 5 when each recursive variable is different; 2. Each call is returned once, such as when the factorial of 5 is calculated, each time the recurrence is returned for the next time; 3. In the recursive function, the statements before the recursive call and all levels of the called functions have the same order of execution; 4. In the recursive function, the execution order of the statements after the recursive invocation is the opposite of the order of each called function; 5. There must be a terminating statement in the recursive function.
a sentence summary recursion: self-invocation and has completed state.
examplesXiao Ming in order to learn English, need to remember the words every day, the first day to remember one, the next day to remember 2 and so on, and so on, please use code to finish, calculate how many words Xiao Ming will start the 10th day? Analysis:The back wall is "the first day to remember one."The recurrence relationship is "Nth day of the word = number of words in n-1 days +n"
#include <stdio.h>
/ * Define a function to get the number of words * /int getwordnumber (n){ if (n = = 1) {return 1; Push back wall }else{return Getwordnumber (n-1) +n; Recursive relationships }}int main (){int num = Getwordnumber (10); Gets the number of words that will beprintf ("Xiao Ming's 10th day:%d words. \ n ", num);return 0;}
examples5 people sat together and asked how old the 5th person was. He said he was 2 years older than the 4th one. Asked the age of 4th, he said he was 2 years older than the 3rd man. asked the 3rd person, and said that the 2nd People's Congress two years old. Ask the 2nd person, say two years old than the 1th person. Finally asked a 1th person, he said is 10 years old. How old is the 5th one?
Program Analysis:
Recursion is divided into two stages , recursive and recursive, using recursive method. If you want to know the age of the 5th person, you need to know the age of the first 4, and so on, push to the 1th person (10 years old), and then push back.
#include <stdio.h>/** Please use recursive function to complete the subject* Small series has put the correct code on the left side of the task "do not know how to do"* Small series hope that all children's shoes independently completed OH ~ */ //define a function, transfer the number of people to go in, return the number of the employee's age. int Getage (numpeople){//Define the age of the returnint age;//If it is a 1th person, the age of 10 yearsif (numpeople==1)age=10; This is the push back wall, which is the condition of ending recursion. Else//has not touched the back-push wall, it calls itself, the recursion. Age = Getage (numPeople-1) +2; Age equals the age of the previous person plus 2 .return age;}
int main (){ printf ("The 5th person's age is%d years", Getage (5));return 0;}
examplesThe first day the monkey picked off n Peaches, then ate half, still not enjoyable, and ate one more. The next day he ate half the remaining peaches and ate one more. After every day, eat the half of the rest of the night before. On the 10th day when you want to eat a peach left, asked the first day how many peaches? and reverse print the number of peaches remaining per day.
#include <stdio.h>
//Define a function, enter Nth day, return the number of peaches remaining on that dayint getpeachnumber (n){int num; Define the number of peaches leftif (n==10) {Num=1; Recursive end condition, which is the push-back wallreturn num; }Else {num = (Getpeachnumber (n+1) + 1) * 2; Recursive relationshipsprintf ("%d peaches left on%d days", N, num);//days, number of peaches left }return num;}int main (){int num = getpeachnumber (1);printf ("The monkey picked the first day:%d peaches.") \ n ", num);return 0;}
Some examples of self-recursion in C language