Hanoi Tower problem C + + implementation

Source: Internet
Author: User

#include <iostream.h>

int Gbsx (char & L, Char & R)// function to Change Order

{char T;

T= L;

L= R;

R= T;

Return0;

}

void Move (int n, char A, char B, char c)// Hanoi core function

{

if (n = = 1)

{

cout<< " move " << a << " to " << c << Endl;

}

Else

{Gbsx (b, c); change the order of the back two

Move (N-1, A, b, c); implementing its steps with recursion

Gbsx (b, c); change the order of the two in the back.

Move (1, a, b, c);

Gbsx (A, b); change the order of the previous two

Move (N-1, A, b, c); implementing its steps with recursion

}

}

int sum (int n)// function to calculate the number of steps

{

INTs = 1;

for (int i = 1; I <= n; i++)

{

S= s*2;

}

returns-1;

}

void Main ()// main function

{int n = 1;

cout<< " Please enter the number of plates you have placed " << Endl;

cin>> N; the input value is assigned to N to implement the

cout<< " total " << sum (n) << " step ," << "step to " << Endl;

Move (n, ' A ', ' B ', ' C '); calling functions

}

Hanoi Tower problem C + + implementation

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.