您的位置:首页 > 编程语言 > C语言/C++

学习C/C++语言:递归求解hanoi汉诺塔问题

2014-05-11 15:56 495 查看
#include<stdio.h>
void hanoi(int n,char one,char two,char three);
void move(char a,char b);
void main()
{
int n;
printf("input the number of diskes:\n");
scanf("%d",&n);
printf("the step to moving %d diskes:\n",n);
hanoi(n,'A','B','C');
}
void move(char a,char b)
{
printf("%c-->%c\n",a,b);
}
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: