|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #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的第十八天 |
|