Implement Recursive Algorithms in C Language

Source: Internet
Author: User
The C language implements recursive algorithms. recursive algorithms are used in function calls. The concept of recursion: calling a function in a function is called a recursive call of a function. Principle of recursive call: After a function meets certain conditions, it does not call itself or end, or call other functions. Otherwise, it is prone to an endless loop, and the program will be paralyzed. We know F (1) = 1, F (0) = 0, FN (n) = f (n-1) + f (n-2), how much is F (5? Step 1: Bring 5 into the recursive formula to obtain F (5) = f (4) + f (3). At this time, F (4) and F (3) are still unknown, you must continue to evaluate F (4) and F (3 ). Step 2: Bring 4 into the recursive formula to obtain F (4) = f (3) + F (2 ). Step 3: Bring 3 into the recursive formula as medium to F (3) = F (2) + F (1 ).

Step 4: Bring 2 into the recursive formula to obtain F (2) = F (1) + f (0 ).

Step 5: F (0) and F (1) are both known at this time, and then the next step is to place their first-level values into the above formula and find F (2) one by one ), F (3), (4), and finally obtain the result F (5 ).

Returns the factorial n! = N * (n-1) * (n-2) *... * 2*1. First, define the final termination condition F (1) = 1. Then define the recursive formula F (n) = f (n) * F (n-1 ). # Include <stdio. h> int FN (int A); void main ()
{
Int sum;
Sum = FN (3 );
Printf ("% d/N", sum );
} Int FN (int)
{
If (A = 1)
{
Return 1;
}
Else
{
A = A * fN (A-1 );
Return;
}}

The number of the Fibonacci series is 1 and the number of the second is the sum of the first two. For example, (, 5 ,...) Calculate the first 10 of the Fibonacci series. First define the final termination condition F (1) = 1, F (2) = 1; then define the recursive formula F (n) = f (n-1) + f (n-2 ). # Include <stdio. h> int FN (int A); void main ()
{
Int sum;
Sum = FN (7 );
Printf ("% d", sum );
} Int FN (int)
{
If (A <= 2 & a> 0)
{
Return 1;
}
Else if (a> 2)
{
A = FN (A-1) + FN (A-2 );
Return;
}
}

A farm bought a newborn ox in the first year. This ox gave birth to a calf in the fourth year. In the future, this ox will have a calf each year. When these calves grow to the fourth place, they will have a new calf. In the future, they will also have a new ox every year. If the ox does not die, this is repeated. How many cows will this farm have in 50 years? First define the final termination condition F (4) = 1; then define F (n) = f (n-1) + f (n-3) in the recursive formula ). # Include <stdio. h> int FN (int A); void main ()
{
Int I;
I = FN (20 );
Printf ("% d/N", I );
} Int FN (int)
{
If (A <4 & a> 0)
{
Return 1;
}
Else if (a> = 4)
{
A = FN (A-1) + FN (A-3 );
Return;
}
}

There was a lotus flower pool with a lotus flower at first, and the number of lotus flowers doubled every day. Assume that the lotus flower will never fade. During the last 30 days, the lotus flower pool is full of lotus flowers. What is the fraction of the lotus flower pool in the last 23rd days? First define the final termination condition F (1) = 1; then define F (n) = f (n-1) * 2 in the recursive formula. # Include <stdio. h> int FN (int A); void main ()
{
Int;
Int B;
Double C;
A = FN (30 );
B = FN (23 );
C = (double) B/;
Printf ("%. 20lf/N", C );
} Int FN (int)
{
If (A = 1)
{
Return 1;
}
Else
{
A = FN (A-1) * 2;
Return;
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.