C recursion
Recursion refers to the method of using the function itself in the definition of the function.
For example:
Once upon a time, there was a mountain, and there was a temple in the mountain. In the temple, there was an old monk who was telling stories to the young monk! What is the story? "Once upon a time there was a mountain, and there was a temple in the mountain. In the temple, there was an old monk who was telling a story to the young monk! What is the story?" Once upon a time, there was a mountain, and there was a temple in the mountain. Tell the little monk a story! What is the story?...'"
The syntax format is as follows:
void recursion()
{
statements;
... ... ...
recursion(); /* 函数调用自身 */
... ... ...
}
int main()
{
recursion();
}
The C language supports recursion, that is, a function can call itself. But when using recursion, programmers need to pay attention to define a condition for exiting from the function, otherwise it will enter an infinite loop.
Recursive functions play a vital role in solving many mathematical problems, such as calculating the factorial of a number, generating the Fibonacci sequence, and so on.
Fibonacci sequence
The Fibonacci sequence, also known as the golden section sequence, refers to such a sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Mathematical recursion representation: F(0)=0, F(1)=1, F(n)=F(n - 1)+F(n - 2) (n ≥ 2, n ∈ N)
Core code breakthrough point: F(0)=0, F(1)=1, F(n)=F(n - 1)+F(n - 2) (n ≥ 2, n ∈ N)
Question: Find the nth Fibonacci sequence
Input: Enter an integer n, representing the nth item of the Fibonacci sequence (0≤n≤20)
Output: output an integer representing the value of the nth item of the Fibonacci sequence
. Example:
Input: 3
Output: 2
Recursive implementation:
#include <stdio.h>
int fibonacci(int n)
{
if(n <= 2)
{ return 1; }
else
{ return fibonacci(n - 1) + fibonacci(n - 2); }
}
int main()
{ int n;//第n项
scanf("%d", &n);
printf("%d\n", fibonacci(n));//第n项斐波那契数列的值
return 0;
}
Naturally, it can also be realized by means of loops, etc., so I won’t make any more statements here.
Find the factorial of n
The factorial of a positive integer is the product of all positive integers less than or equal to that number, and the factorial of 0 is 1. The factorial of a natural number n is written as n!.
Core code breakthrough point:
greater or equal to:
The factorial representation of any natural number n greater than or equal to 1:
n!=1×2×3×…×(n-1)×n
or
n!=n×(n-1)!
Factorial of 0:
0!=1。
Question: Find the factorial of n
Input: Enter an integer n (0≤n≤10)
Output: Output a number representing the factorial of n
Example:
Input: 5
Output: 120
#include<stdio.h>
int Fact(int n)//递归函数
{
if( n <= 1)
{ return 1; }
return n * Fact(n - 1);
}
int main()//主函数
{
int n;//阶乘数
scanf("%d", &n);
printf("%d\n", Fact(n));
return 0;
}
prints an integer with each bit
Problem: Output each bit of an integer
Input: Enter an integer a (size is within the range of int data)
Output: output each digit separated by spaces
Example:
Input: 13579
Output: 1 3 5 7 9
#include<stdio.h>
void Prt(int a)//递归函数
{
if(a > 9)
Prt(a / 10); //单个语句将{}省略
printf("%d ", a % 10);
}
int main()//主函数
{
int a ;
scanf("%d",&a);
Prt(a);
printf("\n");
return 0;
}