汉诺塔-算法实现

来源:互联网 发布:单片机串口通讯实验 编辑:程序博客网 时间:2024/06/11 04:35

使用递归方法来实现经典算法问题<汉诺塔问题>。

 

下面是本人的代码:

 

 

 

// 汉诺塔.cpp : Defines the entry point for the console application.
//将A中的圆块移动到C中。

#include "stdafx.h"

#include<stdio.h>

 

void move(char,char);

void move(char,char);

 

void main()
{
 int m;
 printf("input the number of disks:");
 scanf("%d",&m);
 printf("the step to moving %3d diskes:/n",m);
 hanoi(m,'A','B','C');
}

 

void move(char x,char y)
{
      printf("%c-->%c/n",x,y);
}
void hanoi(int n,char one ,char two,char three)
{
 if(n==1)
     move(one,three);
 else
 {
      hanoi(n-1,one,three,two);
      move(one,three);
      hanoi(n-1,two,one,three);
 }
}

原创粉丝点击