-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <stdio.h> | ||
|
||
void hanoi(int n, char a,char b,char c); | ||
void move(int n, char a, char b); | ||
|
||
int count; | ||
|
||
int main() | ||
{ | ||
int n=8; | ||
printf("请输入汉诺塔的层数:"); | ||
scanf("%d",&n); | ||
hanoi(n, 'A', 'B', 'C'); | ||
return 0; | ||
} | ||
|
||
void hanoi(int n, char a, char b, char c) | ||
{ | ||
if (n == 1) //把第n个盘子由A移到C | ||
{ | ||
move(n, a, c); | ||
} | ||
else //把n-1个盘子由A移到B | ||
{ | ||
hanoi(n - 1, a, c, b); | ||
move(n, a, c); | ||
hanoi(n - 1, b, a, c); | ||
} | ||
} | ||
|
||
void move(int n, char a, char b) | ||
{ | ||
count++; | ||
printf("第%d步:从%c移到%c\n",count,a,b); | ||
} |