#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void Hanoi(int n,char a,char b,char c)
{
if(n==1)
{
printf("%d号金片:%c-->%c\n",n,a,c);
}else
{
Hanoi(n-1,a,c,b);
printf("%d号金片:%c-->%c\n",n,a,c);
Hanoi(n-1,b,a,c);
}
}
int main()
{
int n;
printf("汉诺塔问题\n");
printf("请输入n=");
scanf("%d",&n);
if(n<=0)
{
printf("n必须为正整数!\n");
exit(-1);
}
Hanoi(n,'A','B','C');//把n个金片从针'A'经由针'B'移动到针'C'
return 0;
}
#include<math.h>
#include<stdlib.h>
void Hanoi(int n,char a,char b,char c)
{
if(n==1)
{
printf("%d号金片:%c-->%c\n",n,a,c);
}else
{
Hanoi(n-1,a,c,b);
printf("%d号金片:%c-->%c\n",n,a,c);
Hanoi(n-1,b,a,c);
}
}
int main()
{
int n;
printf("汉诺塔问题\n");
printf("请输入n=");
scanf("%d",&n);
if(n<=0)
{
printf("n必须为正整数!\n");
exit(-1);
}
Hanoi(n,'A','B','C');//把n个金片从针'A'经由针'B'移动到针'C'
return 0;
}