zoukankan
html css js c++ java
睡不着,随便写了下汉诺塔的解决方法。
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(
string
[] args)
{
int
times
=
new
HanoiTower(
3
).SolveTower();
Console.WriteLine(
"
一共需要{0}次
"
, times);
Console.ReadKey();
}
}
class
HanoiTower
{
private
int
plates;
public
HanoiTower(
int
plates)
{
this
.plates
=
plates;
}
public
int
SolveTower()
{
return
SolveTower(plates,
"
A
"
,
"
B
"
,
"
C
"
);
}
private
int
SolveTower(
int
plates,
string
a,
string
b,
string
c)
{
int
count
=
0
;
if
(plates
<=
0
)
throw
new
ArgumentOutOfRangeException(
"
plates
"
,
"
盘子数必须是大于0的整数
"
);
if
(plates
==
1
)
{
move(a, c);
return
1
;
}
count
+=
SolveTower(plates
-
1
, a, c, b);
count
+=
SolveTower(
1
, a, b, c);
count
+=
SolveTower(plates
-
1
, b, a, c);
return
count;
}
private
void
move(
string
a,
string
c)
{
Console.WriteLine(a
+
"
=>
"
+
c);
}
}
}
张旋(zxsoft)
如对本文有什么疑问,请在下面写下留言,谢谢!
查看全文
相关阅读:
值类型和引用类型
Visual Staudio 2015 打开指定文件,定位到指定文件目录下
c# datarow[] 转换成 datatable, List<T> 转datatable
存储过程存储过程需要用两个'',先where再Group,再Order by
SQL之case when then用法
select 1 from table 语句中的1代表什么意思
SqlServer使用表值函数汇总
SQLServer2008或SQLServer2008 R2没有智能提示解决方法
Linux下./configure && make && make install 编译安装和卸载
Linux下web服务的搭建
原文地址:https://www.cnblogs.com/zxsoft/p/1210400.html
最新文章
NDK学习笔记-JNI的异常处理与缓存策略
将本地已经存在的非git项目提交到github上的空仓库
windows ionic bash: command not found
vim全选复制粘贴
curl http code 0
postman请求失败
ionic 入门创建第一个应用demo
sftp协议下如何上传和下载文件
php json格式化输出
vue.js安装问题
热门文章
ubuntu部分端口命令的使用----开启端口/开启防火墙
nodejs接收post请求参数
http协议里定义的四种常见数据的post方法
正则匹配换行符、英文、非英文(比如中文)
node端console.log输出不同颜色文字
[笔记] nice-upload 与 nice-static-server
记录一些好用的工具 | 下载网址
线上画流程图
《如何做好团队技术分享》-一篇好文章
MongoDB导入导出以及数据库备份
Copyright © 2011-2022 走看看