当前位置:网站首页>《程序设计基础》 第十章 函数与程序结构 7-2 汉诺(Hanoi)塔问题 (20 分)
《程序设计基础》 第十章 函数与程序结构 7-2 汉诺(Hanoi)塔问题 (20 分)
2022-07-20 16:33:00 【茶然o】
古代某寺庙中有一个梵塔,塔内有3个座A、B和C,座A上放着64个大小不等的盘,其中大盘在下,小盘在上。有一个和尚想把这64 个盘从座A搬到座B,但一次只能搬一个盘,搬动的盘只允许放在其他两个座上,且大盘不能压在小盘上。现要求用程序模拟该过程,输入一个正整数n,代表盘子的个数,编写函数
void hanoi(int n,char a,char b,char c)
其中,n为盘子个数,从a座到b座,c座作为中间过渡,该函数的功能是输出搬盘子的路径。
输入格式:
输入在一行中给出1个正整数n。
输出格式:
输出搬动盘子路径。
输入样例:
3
结尾无空行
输出样例:
a-->b
a-->c
b-->c
a-->b
c-->a
c-->b
a-->b
结尾无空
#include<stdio.h>
#include<stdlib.h>
void hanio(int n,char a,char b,char c);
int main()
{
int n=0;
scanf("%d",&n);
hanio(n,'a','b','c');
return 0;
}
void hanio(int n,char a,char b,char c)
{
if(n==1)
printf("%c-->%c\n",a,b);
else{
hanio(n-1,a,c,b);
printf("%c-->%c\n",a,b);
hanio(n-1,c,b,a);
}
}
边栏推荐
- Ncnn OP forward code learning
- 关于开放封闭原则
- Flex布局
- tvm安装与使用
- Wechat applet 04 route jump and life cycle function
- mmpose使用笔记
- Musk: I uploaded my brain to the cloud. Sorry, 404
- 416. 分割等和子集
- TNN notes
- What if there are more than 1 million messages in the message queue of highly paid programmers & interview questions series 126? How to ensure the consistency of messages?
猜你喜欢
Apprentissage de la sécurité des réseaux (vii) IIS
Musk: I uploaded my brain to the cloud. Sorry, 404
VC all rested? In the first half of the year, Hillhouse venture capital made nearly 80 shots, 60% before the a-round
Zhang Xiaoquan, are you wronged?
kubernetes调度概念与工作流程
AI helps double carbon goal: let every kilowatt hour be optimized by us
张小泉,冤吗?
Separable Convolution可分离卷积
Error when wmware enables virtualization function
网络安全学习(三)基本DOS命令
随机推荐
2021/7/16 学习散射网络第一步-神经网络入门
在同花顺上登录证券账号安全吗
过滤列表
OptaPlanner 发展方向与问题
Network Security Learning (VIII) domain
Understand the secondary node of industrial Internet identity analysis
微信小程序 21 完善视频页②
李宏毅老师2020年深度学习系列讲座笔记4
深度学习基础与实践课程笔记0&1
半导体新晋独角兽,TCL刚刚投了18亿
需研究的东东
VC 都歇了?上半年高瓴创投出手近 80 次,六成 A 轮前
php 获取缩略图
How to effectively avoid code being "poisoned"?
试着换个角度理解低代码平台设计的本质
李宏毅老师2020年深度学习系列讲座笔记1
交叉熵损失
Compile, install and configure PHP under Debian 9
什么是视频内容推荐引擎?
Ncnn OP forward code learning