河内塔

#include<stdio.h>
void towers(int,char,char,char);
int main()
{
    int num;
    printf("Enter the number of disks : ");
    scanf("%d", &num);
    printf("The sequence of moves involved in the Tower of Hanoi are :
");
    towers(num,'a','b','c'); //将a柱子上的盘子移到b柱子上,借助于c柱子
    getchar();
    getchar();
    return;
}
void towers(int num, char frompole, char topole, char auxpole)
{
    if(num==1) printf("move disk 1 from pole %c to pole %c
", frompole, topole);
    else 
    {
        towers(num-1,frompole,auxpole,topole); //将a柱子上的num-1个盘子移到c柱子上,借助于b柱子
        printf("move disk %d from pole %c to pole %c
", num, frompole, topole); //将a柱子上的最大的盘子移动到b柱子上
        towers(num-1,auxpole,topole,frompole);//将c柱子上的num-1个盘子移动到b柱子上,借助于a柱子
    }
}

运行结果

原文地址:https://www.cnblogs.com/learning-c/p/5206116.html