您的位置:首页 > 其它

汉诺塔-算法实现

2010-09-18 19:03 211 查看
使用递归方法来实现经典算法问题<汉诺塔问题>。

 

下面是本人的代码:

 

 

 

// 汉诺塔.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);
 }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: