S1E35:汉诺塔
#include <stdio.h>//鱼C论坛——BaysideLizard写于2023年11月18日
void hanoi(int,char,char,char);
int main()
{
int n;
printf("请输入汉诺塔的层数:");
scanf("%d",&n);
hanoi(n,'x','y','z');
return 0;
}
void hanoi(int n,char x,char y,char z)
{
if(n == 1)
{
printf("%c --> %c\n",x,z);
}
else
{
hanoi(n-1,x,z,y);
printf("%c --> %c\n",x,z);
hanoi(n-1,y,x,z);
}
}
运行结果:
请输入汉诺塔的层数:3
x --> z
x --> y
z --> y
x --> z
y --> x
y --> z
x --> z
Process returned 0 (0x0) execution time : 1.131 s
Press any key to continue.
在FishC学C的第十八天
页:
[1]