递归函数的经典例子(汉诺塔问题)

#include
void HN(int n,char a,char b,char c);
int main(int argc,char *argv[])
{
 int Dish_num;
 printf(“Please Input the Dish num: “);
  scanf(“%d”,&Dish_num);
 while(Dish_num != 0){
  printf(“%d个盘子的移动步骤如下:\n”,Dish_num);
  HN(Dish_num,’A’,’B’,’C’);
  printf(“Please Input the Dish num: “);
  scanf(“%d”,&Dish_num);
 }
 while(1);
 return 0;
}
void HN(int m,char a,char b,char c)
{
 if(m==1){
  printf(“From %c to %c\n”,a,c);
 }
 else{
  HN(m-1,a,c,b);
   printf(“From %c to %c\n”,a,c);
  HN(m-1,b,a,c);
 }
}

    原文作者: 汉诺塔问题
    原文地址: https://blog.csdn.net/u014100559/article/details/46592971
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞