#include <stdio.h>
#include <conio.h>
int main(){
void hanoi(int n,char one,char two,char three);
int m;
printf("输入层数");
scanf("%d",&m);
printf("移动%d层的步骤为:\n",m);
hanoi(m,'A','B','C');
getch();
}
void hanoi(int n,char one,char two,char three){
void move(char x,char y);
if(n==1)
move(one ,two);
else{
hanoi(n-1,one,two,three);
move(one,three);
hanoi(n-1,two,one,three);
}
}
void move(char x,char y){
printf("%c-->%c\n",x,y);
#include <conio.h>
int main(){
void hanoi(int n,char one,char two,char three);
int m;
printf("输入层数");
scanf("%d",&m);
printf("移动%d层的步骤为:\n",m);
hanoi(m,'A','B','C');
getch();
}
void hanoi(int n,char one,char two,char three){
void move(char x,char y);
if(n==1)
move(one ,two);
else{
hanoi(n-1,one,two,three);
move(one,three);
hanoi(n-1,two,one,three);
}
}
void move(char x,char y){
printf("%c-->%c\n",x,y);
}
本文介绍了一个用C语言实现的汉诺塔问题解决方案。通过递归算法演示了如何将不同大小的圆盘从一个柱子移动到另一个柱子上,并确保每次只能移动一个圆盘且大盘不能放在小盘之上。
1045

被折叠的 条评论
为什么被折叠?



