Hanoi(汉诺)塔问题

来源:互联网 发布:php适配器模式 编辑:程序博客网 时间:2024/05/21 06:21

//古代有一个梵塔,塔内有3个座A,B,C,A上有4个盘子,老和尚想把这64个盘子移到C座,每次只移动一个盘,且在移动过程中3个座上始终保持大盘在下,小盘在上。

#include<stdio.h>int main(){void hanoi(int n, char one, char two, char three);int num;printf("Please input the number of disk:");scanf("%d",&num);printf("The step to move %d disk:\n", num);hanoi(num,'A','B','C');}void hanoi(int n, char one, char two, char three ){void move(char x, char y);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){printf("%c-->%c\n",x,y);}