The recursive method of the thought of C + + basic algorithm thought _c language

Source: Internet
Author: User

Recursive method is a very common algorithm, which is widely used in the fields of mathematics calculation. The recursive method is suitable for the occasion with obvious formula law.

The basic idea of recursive method
Recursive algorithm is a rational thinking of Morse's representative, according to the existing data and relationships, gradually pushed to get results. The execution process of the recursive method is as follows:

(1) To solve the intermediate result according to the known result and relation.

(2) Determine whether to meet the requirements, if not achieved, then continue to solve the intermediate results based on known results and relationships. If the requirement is met, a correct answer is found.

The recursive algorithm requires the user to know the logical relationship between the answer and the problem. In many mathematical problems, there are definite formulas to follow, so the recursive method can be used to achieve.

Recursive Method Example
The Fibonacci sequence in mathematics is a classic example of using recursive methods.

The 13th century Book of Abacus, an Italian mathematician, chronicles the typical issue of rabbit litter, with the following effect:

If a pair of one-month-old rabbit after every one months can give birth to a small rabbit, and a newborn rabbit born two months before they can give birth to a small rabbit. That is, January was born, March began to litter. So assuming that no rabbit deaths have been produced in a year, how many pairs of rabbits are there in 1 years?

1. Recursive algorithm
Let's analyze the rabbit litter problem. Let's look at the logarithm of the rabbit every month.

First month: 1 pairs of rabbits;

Second month: 1 pairs of rabbits;

Third month: 2 pairs of rabbits;

Fourth month: 3 pairs of rabbits;

Fifth month: 5 pairs of rabbits;

Sixth month: 8 pairs of rabbits;

..................

As can be seen from the above, from the beginning of the third month, the total number of rabbits per month equal to the first two months the sum of rabbits. The corresponding calculation formula is as follows:

The number of rabbits in the nth month fn=fn-1+fn-2.

Here the first month of the rabbit number f1=1, the second month of the rabbit number f2=1.

can be solved with recursive formulas. For general convenience, we can write an algorithm to calculate the Fibonacci sequence problem, according to this thinking to write the corresponding rabbit litter problem solving algorithm, the sample code is as follows:

Copy Code code as follows:

/*
Input parameter n is the time of experience (month), in which the Fibonacci sequence is computed by recursive invocation.
*/
int Fibonacci (n)
{
int t1,t2;
if (n>0)
{
if (n==1| | n==2)
{
return 1;
}
Else
{
T1=fibonacci (n-1);
T2=fibonacci (n-2);
return t1+t2;
}
}
Else
{
return 0;
}
}

recursive algorithm to solve litter problem in rabbits
With the above algorithm, we can solve the problem of any kind in the rabbit litter problem. Here is a complete rabbit litter Problem Solver code:
Copy Code code as follows:

#include <iostream>
using namespace Std;
/*
Input parameter n is the time of experience (month), in which the Fibonacci sequence is computed by recursive invocation.
*/
int Fibonacci (int n)
{
int t1,t2;
if (n>0)
{
if (n==1| | n==2)
{
return 1;
}
Else
{
T1=fibonacci (n-1); Recursive call to get F (n-1)
T2=fibonacci (n-2); Recursive call to get F (n-2)
return t1+t2;
}
}
Else
{
return 0;
}
}
int main ()
{
int n,num;
cout<< "Recursive Algorithm for rabbit litter problem:" <<endl;
cout<< "Please enter the time:" <<endl;
cin>>n;
Num=fibonacci (n);
cout<< "after" <<n<< "months" <<endl;
cout<< "The number of rabbits is:" <<num<< "to" <<endl;
return 0;
}

Execute the program, user input 12, get the result as shown:

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.