汉诺塔

来源:互联网 发布:淘宝登陆不了 编辑:程序博客网 时间:2024/06/17 22:28
输入n表示盘子的个数,只能小的在上,大的在下,求将第一个移到第三个的
#include <stdio.h>int main(){  void hanoi(int n,char one,char two,char three);         // 对hanoi函数的声明   int m;  printf("input the number of diskes:");  scanf("%d",&m);  printf("The step to move %d diskes:\n",m);  hanoi(m,'A','B','C');}void hanoi(int n,char one,char two,char three)          // 定义hanoi函数      // 将n个盘从one座借助two座,移到three座  {  void move(char x,char y);       // 对move函数的声明   if(n==1)     move(one,three);  else    {     hanoi(n-1,one,three,two);     move(one,three);     hanoi(n-1,two,one,three);    } } void move(char x,char y)           //  定义move函数  {   printf("%c-->%c\n",x,y); }

最小步移法。
原创粉丝点击