Classical recursion-the Fibonacci series, the Tower of Hanoi, And the Fibonacci Tower

Source: Internet
Author: User

Classical recursion-the Fibonacci series, the Tower of Hanoi, And the Fibonacci Tower

Fibonacci

Tower of Hanoi

0 1 1 2 3 5 8 13 21

int fibonacci(int a){    if(a==0)        return 0;    else if(a==1)        return 1;    else        return fibonacci(a-1)+fibonacci(a-2);}

I also want to tune up the tower:

First, let's talk about how to play this thing.

What needs to be done is to put the plate on the first needle on the third needle.

It is required that only one plate be moved at a time, and only a large plate can be placed at the bottom and a small plate can be placed at a time, for every two plates.

So to achieve this purpose

Step 1: Put the last plate on the needle on the top of the 3 needle. // Move

[Premise: in this case, we need to put the n-1 plate on a 2-pin plate first. That is, STEP 0 :]

Step 0: in this case, you need to put the n-1 plate on the 2-pin plate first. // Logic

 

// For the code, this sentence is essentially equivalent to 2 needles as the target, that is, 2 needles on the plate. Therefore, 2 needles are the target needles. We need to place n-1 plates from 1 to 2. With 3 needles, the relationship between abc and 3 needles is in hanoi (n, a, B, c) the essence is that a is used as the removal needle, B is used as the aid needle, and c is used as the target needle. Therefore, this sentence should be written: hanoi (n-1, a, c, B );

Step 1:

Step 2: the premise of The N-2 plate to 1 needle. // Logic

So from 2, put it on 1, with 3. These parameters are 213.

Step 3: Put the last plate on the two pins on the Three pins. // Move

 

Then put the n-3 plate above 2 needles, That Is Step0:

Then step 1

So it is probably like this:

Void hanoi (int n, int a, int B, int c) {if (n = 1) printf ("% d-> % d \ n",, c); // move else {hanoi (n-1, a, c, B); printf ("% d-> % d \ n", a, c ); // move hanoi (n-1, B, a, c );}}

Running result:

Code:

/** # Include <iostream> int fibonacci (int); void hanoi (int n, int a, int B, int c); int main (int argc, char ** argv) {// printf ("% d", FIG (1); hanoi (3, 1, 2, 3); return 0;} void hanoi (int n, int a, int B, int c) {if (n = 1) printf ("% d-> % d \ n", a, c ); // move else {hanoi (n-1, a, c, B); printf ("% d-> % d \ n", a, c ); // move hanoi (n-1, B, a, c) ;}/ ** returns the size of the number a, starting from 0. */Int fibonacci (int a) {if (a = 0) return 0; else if (a = 1) return 1; else return fibonacci (A-1) + maid (A-2 );}*/

Try four:

 

There should be no problem if the eyes move for a moment.

Let's review the Code:

/** N indicates the number of plates to be moved. A Indicates from which the needle is removed. Removing needle B indicates which needle is used. The needle c Represents the needle to be moved. Target needle */void hanoi (int n, int a, int B, int c) {if (n = 1) printf ("% d-> % d \ n ", a, c); // move else {hanoi (n-1, a, c, B); // logic printf ("% d-> % d \ n",, c); // move hanoi (n-1, B, a, c); // logic }}

 

If there is only one plate, move the plate from 1 to 3.

Otherwise, we will move n-1 dishes to 2.

Then put the plate on 3.

Move the remaining plate from 2 to 3.

 

You can start to write it silently.

Hanoi(int n,int from,int rent,int destination){    If(n==1)     printf("%d -> %d",from,destination);    Else{    Hanoi(n-1,from,destination,rent);    Printf("%d ->%d",from,destination);    Hanoi(n-1,rent,from,destination);}

If you don't read it, you will still forget it. In short, this is the case, algorithm ~ Most of them are backed up. There are so many real people...

 

Source code:

# Include <iostream> int fibonacci (int); void hanoi (int n, int a, int B, int c); int main (int argc, char ** argv) {// printf ("% d", fibonacci (1); hanoi (,); return 0 ;}/ ** n indicates the number of plates to move. A Indicates from which the needle is removed. Removing needle B indicates which needle is used. The needle c Represents the needle to be moved. Target needle */void hanoi (int n, int a, int B, int c) {if (n = 1) printf ("% d-> % d \ n ", a, c); // move else {hanoi (n-1, a, c, B); // logic printf ("% d-> % d \ n",, c); // move hanoi (n-1, B, a, c); // logic}/** returns the size of the number a, starting from 0. */Int fibonacci (int a) {if (a = 0) return 0; else if (a = 1) return 1; else return fibonacci (A-1) + maid (A-2 );}

 

 

Related Article

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.