Skip to content

Commit

Permalink
完成了hanoi
Browse files Browse the repository at this point in the history
  • Loading branch information
Kitaibel committed Mar 13, 2018
1 parent ac395a5 commit bb31cc9
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions level1/p08_hanoi/hanoi_Kitaibel.c
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);
}

0 comments on commit bb31cc9

Please sign in to comment.