Created
May 28, 2015 09:28
-
-
Save misakar/7902a184c45499264938 to your computer and use it in GitHub Desktop.
汉洛塔问题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* | |
* 汉诺塔问题 | |
* |-- n=1 move(A,C) | |
* hanoi(n,A,B,C)-|-- n>1 hanoi(n-1,A,C,B) | |
* | move(A,C) | |
* hanoi(n-1,B,A,C) | |
*/ | |
# include<stdlib.h> | |
# include<stdio.h> | |
# include<conio.h> | |
char move(char x,char y); | |
void hanoi(int n,char A,char B,char C); | |
int main() | |
{ | |
int n; | |
scanf("%d",&n); //输入塔盘的个数 | |
printf("移动%d个盘子的步骤是\n",n); | |
hanoi(n,'A','B','C'); | |
return 0; | |
} | |
void hanoi(int n,char A,char B,char C){ | |
//解体步骤(参考文件头部) | |
//递归调用 | |
if (n == 1) move('A','C'); | |
else{ | |
hanoi(n-1,'A','C','B'); | |
move('A','C'); | |
hanoi(n-1,'B','A','C'); | |
} | |
} | |
char move(char x,char y) | |
{ | |
printf("%c -------------> %c\n",x,y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment