如果对这数字没什幺概念,就假设每秒钟搬一个盘子好了,也要约5850亿年左右。
#include
void hanoi(int n, char A,char B,char C)
{
if(n == 1)
{
printf("Move sheet %d from %c to %c\n", n, A,C);
}
else
{
hanoi(n-1,A,C,B);
printf("Move sheet %d from %c to %c\n", n, A,C);
hanoi(n-1,B,A,C);
}
}
int main()
{
int n;
printf("请输入盘数:");
scanf("%d", &n);
hanoi(n,'A', 'B', 'C');
return 0;
}
打印移动的次数
#include
int count=0;
void fun(int n,char a,char b,char c)
{
if(n==1)
{
count++;
}
else
{
fun(n-1,a,c,b);
count++;
fun(n-1,b,a,c);
}
}
int main()
{
int n;
printf("请输入盘数:");
scanf("%d",&n);
fun(n,'X','Y','Z');
printf("%d\n",count);
return 0 ;
}