Hanoi Tower of Hanoi

Source: Internet
Author: User

If the pillar is labeled A,b,c, move from A to C, and move it directly to C when there is only one plate; when there are two plates, B is used as the auxiliary column; If the number of disks is more than 2, it is very simple to cover up the second plate, and it is easy to handle two plates at a time, namely: A->b, A- >c, b->c The three steps, and the part that is covered up, in fact, by the recursive processing of the equation.

The code is as follows:


#include <stdio.h>void Hanoi (int n,char A,char B,char C) {if (n = = 1) {printf ("Move sheet%d from%c to%c\n", n,a,c);} Else{hanoi (n-1,a,c,b);p rintf ("Move sheet%d from%c to%c\n", N,a,c); Hanoi (N-1,B,A,C);}} int main () {int n;printf ("Please enter the number of disks:"), scanf ("%d", &n), Hanoi (N, ' A ', ' B ', ' C '); return 0;}

Operation Result:

Copyright NOTICE: This article is iflei original article, reprint please indicate source.

Hanoi Tower of Hanoi

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.