[C Language/C ++] Recursive Algorithm

Source: Internet
Author: User

Recursive Algorithms are also simple and commonly used algorithms in C Language algorithms. In this article, we will talk about recursive algorithms. First, let's take a look at what recursive algorithms are. There is only one sentence for the concept of recursive algorithms: A process (or function) calls itself directly or indirectly. Such a process (or function) is called a recursive process (or function ).. Let's take a look at the features of recursive algorithms: (1) recursion is to call itself in a process or function. (2) When using a recursive policy, there must be a clear recursive termination condition called the recursive exit. (3) recursive algorithms are usually simple in solving problems, but they are less efficient in solving problems. Therefore, recursive algorithms are generally not recommended for programming. (4) In the process of recursive calling, the system opens a stack for storing the return points and local volumes of each layer. Too many recursion times may cause stack overflow. Therefore, recursive algorithms are generally not recommended for programming. Let's look at the recursive classification: In the direct recursive program design, a process or function calls itself directly or indirectly, it is called a recursive call. Subprograms call themselves directly, which is called direct recursion. subprograms A and B of the nested relationship, and B of the inner layer call A of the outer layer, which is indirect low conversion. subprograms A and B of the level-level relationship, where A calls B and B calls A, which is indirect recursion. However, this indirect recursion uses the rule of "Advanced reference. Below, the blogger finds some questions about recursive algorithms. Let's take a look at the first question. This is a classic question: factorial. I believe most people know what a factorial is, but if you do not know it, refer to Baidu Encyclopedia: factorial. Well, in fact, this question is very simple. We only need a function to solve the problem. Copy code 1 int recursive (int I) 2 {3 int sum = 0; 4 if (0 = I) 5 return (1 ); 6 else7 sum = I * recursive (I-1); 8 return sum; 9} copy the code is the above recursive function, this question I will not waste space to explain, focus on the second example: tower of Hanoi problems. There are three pillars in a temple, and the first has 64 dishes, which are getting bigger and bigger from top down. Ask the old monk in the temple to move all the 64 dishes to the third pillar. When moving, you can only press a small plate on a large plate. You can only move one file at a time. 1. At this time, the old monk (we call him the first monk later) felt very difficult, so he thought: if a person could move the first 63 dishes to the second pillar first, I moved the last plate directly to the third pillar, and then the person moved the first 63 plates from the second pillar to the third pillar. Then my task was completed, simple. So he finds a monk younger than him (we call him the second monk later: ① you move the first 63 dishes to the second pillar ② then I move the first 63 dishes to the third pillar. ③ you move the first 63 dishes to the third pillar. 2. The second one. monk received the task, he thought it was difficult, so he thought like the first MONK: if one person could move the first 62 dishes to the third pillar, I moved the last plate directly to the second pillar, and then the person moved the first 62 plates from the third pillar to the third pillar. Then my task was completed, simple. So he also found a monk younger than him (we call him the third monk later: ① you move the first 62 dishes to the third pillar ② then I move the first 63rd dishes to the second pillar and then ③ you move the first 62 dishes to the second pillar 3. The third monk after receiving the task, the task of moving the first 61 dishes was handed over to the fourth monk according to the gourd words, and so on, until the task has been handed over to 64th monks (it is estimated that 64th monks are very depressed and have no chance to give orders to others, because there is only one plate here ). 4. Submit the task to complete the assignment. Push back: 64th monks move 1st dishes, remove them, and then 63rd monks move the 2nd dishes They allocated to themselves. The third monk moved 64th more dishes to the third. Here, the task of the 64th monks is completed, and the 63rd monks have completed the first step of the task assigned to them by the 62nd monks. It can be seen from the above that only 64th monks have completed their tasks and 63rd monks have completed their tasks. Only after the tasks of 2nd monks-64th monks have been completed, the task of 1st monks can be completed. This is a typical recursive problem. Now we have three plates for analysis: 1st monks command: ① 2nd monks you first move the first two plates of the first pillar to the second pillar. (With the help of the third pillar) ② 1st monks I myself moved the last plate of the first pillar to the third pillar. ③ You move the first two plates from the second pillar to the third pillar. Obviously, the second step is easy to implement. In the first step, the first monk had two plates, and he ordered: ① 2nd monks, and you moved the first pillar of the plate to the third pillar. (With the help of the second pillar) ② 2nd monks I myself moved the 2nd plates of the first pillar to the second pillar. ③ 3rd monks you move 1st dishes from the third pillar to the second pillar. Similarly, the second step is easy to implement, but the first monk only needs to move one plate, so he does not have to dispatch the task. (Note: This is the condition for stopping recursion, also called the Boundary Value) the third step can be decomposed into two plates for the 2nd monks. The command is as follows: ① 3rd monks you move the 1st plates on the second pillar to the first pillar. ② 2nd monks I moved 2nd dishes from the second pillar to the third pillar. ③ 3rd monks move the plate on the first pillar to the third pillar. The combination of analysis is: 1 → 3 1 → 2 3 → 2 use the third column to move to the second column | 1 → 3 | 2 → 1 → 3 → 3 use the first column to move to the third column | seven steps are required. If there are four plates, the first monk's command contains three plates in step 1 and step 2, so each of them requires 7 steps and 14 steps in total, add one step of 1st monks, so four plates need to move 7 + 1 + 7 = 15 steps in total. Similarly, five plates need 15 + 1 + 15 = 31 steps, 31 + 1 + 31 = 64 steps for 6 dishes ...... From this we can know that moving n plates requires (2 to the Npower)-1 step. According to the overall analysis above, we can see that the n plates are moved from one seat (equivalent to the first pillar) to three (equivalent to the third pillar): (1) move one seat (n-1) with 3 to 2. (2) Move the n plates on the first seat to the third seat. (3) Move two (n-1) dishes on one seat to three. The following uses hanoi (n, a, B, c) to move one n plates to three with two. Obviously: (1) hanoi (n-1, 2) (3) is hanoi (n-1, 3) in C language, that is: copy code 1 # include <stdio. h> 2 int method (int n, char a, char B) 3 {4 printf ("number .. % d .. form .. % c .. to .. % c .. "n", n, a, B); 5 return 0; 6} 7 int hanoi (int n, char a, char B, char c) 8 {9 if (n = 1) move (1, a, c); 10 else11 {12 hanoi (n-1, a, c, B); 13 move (n, a, c); 14 hanoi (n-1, B, a, c); 15}; 16 return 0; 17} 18 int main () 19 {20 int num; 21 scanf ("% d", & n Um); 22 hanoi (num, 'A', 'B', 'C'); 23 return 0; 24} copy the Code. This is A recursive algorithm. In C, recursive Algorithms are more practical than enumeration algorithms, but both algorithms are simple.

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.