zoukankan      html  css  js  c++  java
  • 河内塔

    #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柱子
        }
    }

    运行结果

  • 相关阅读:
    第二次安卓作业
    第十一次作业
    第十一次上机练习
    第十次作业
    第十次上机练习
    第九次作业
    第九次上机练习
    添加用户 Android 6
    Android 5
    activity带数据跳转
  • 原文地址:https://www.cnblogs.com/learning-c/p/5206116.html
Copyright © 2011-2022 走看看