博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【三色汉诺塔】
阅读量:5332 次
发布时间:2019-06-14

本文共 2349 字,大约阅读时间需要 7 分钟。

/*三色汉诺塔 */#include 
void hanoi(int disks, char source, char temp, char target){ if(disks == 1) { printf("move disk from %c to %c \n", source ,target); printf("move disk from %c to %c \n", source ,target); printf("move disk from %c to %c \n", source ,target); } else { hanoi(disks - 1, source, target, temp); hanoi(1, source, temp, target); hanoi(disks - 1, temp, source, target); }}void hanoi3colors(int disks){ char source = 'A'; char temp = 'B'; char target = 'C'; int i; if(disks == 3) { printf("move disk from %c to %c \n", source , temp); printf("move disk from %c to %c \n", source , temp); printf("move disk from %c to %c \n", source , target); printf("move disk from %c to %c \n", temp , target); printf("move disk from %c to %c \n", temp , source); printf("move disk from %c to %c \n", target , temp); } else { hanoi(disks / 3 - 1, source, temp, target); printf("move disk from %c to %c \n", source, temp); printf("move disk from %c to %c \n", source, temp); printf("move disk from %c to %c \n", source, temp); hanoi(disks / 3 - 1, target, temp, source); printf("move disk from %c to %c \n", temp , target); printf("move disk from %c to %c \n", temp , target); printf("move disk from %c to %c \n", temp , target); hanoi(disks / 3 - 1, source, target, temp); printf("move disk from %c to %c \n", target , temp); printf("move disk from %c to %c \n", target , temp); hanoi(disks / 3 - 1, temp, source, target); printf("move disk from %c to %c \n", source , temp); for(i = disks / 3 - 1; i > 0; i--) { if(i > 1) { hanoi(i - 1, target, source, temp); } printf("move disk from %c to %c \n", target, source); printf("move disk from %c to %c \n", target, source); if(i > 1) { hanoi(i - 1, temp, source, target); } printf("move disk from %c to %c \n", source, temp); } }}int main(){ int n; printf("请输入盘数:"); scanf("%d", &n); hanoi3colors(n); return 0;}

 

转载于:https://www.cnblogs.com/libra-yong/p/6296346.html

你可能感兴趣的文章
SOAP web service用AFNetWorking实现请求
查看>>
Java变量类型,实例变量 与局部变量 静态变量
查看>>
mysql操作命令梳理(4)-中文乱码问题
查看>>
Python环境搭建(安装、验证与卸载)
查看>>
一个.NET通用JSON解析/构建类的实现(c#)
查看>>
Windows Phone开发(5):室内装修 转:http://blog.csdn.net/tcjiaan/article/details/7269014
查看>>
详谈js面向对象 javascript oop,持续更新
查看>>
关于这次软件以及pda终端的培训
查看>>
jQuery上传插件Uploadify 3.2在.NET下的详细例子
查看>>
如何辨别一个程序员的水平高低?是靠发量吗?
查看>>
新手村之循环!循环!循环!
查看>>
正则表达式的用法
查看>>
线程安全问题
查看>>
SSM集成activiti6.0错误集锦(一)
查看>>
下拉刷新
查看>>
linux的子进程调用exec( )系列函数
查看>>
MSChart的研究
查看>>
C# 索引器
查看>>
MySQLdb & pymsql
查看>>
zju 2744 回文字符 hdu 1544
查看>>