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)
如对本文有什么疑问,请在下面写下留言,谢谢!
查看全文
相关阅读:
win10上使用linux命令
leetcode--js--Median of Two Sorted Arrays
leetcode--js--Longest Substring Without Repeating Characters
Linux常用的命令
微信小程序
leetcode—js—Add Two Numbers
PHPExcel使用
console控制台的用法
git中常混淆的操作
mysql解析json下的某个字段
原文地址:https://www.cnblogs.com/zxsoft/p/1210400.html
最新文章
1050. String Subtraction (20)
1083. List Grades (25)
1030. 完美数列(25)
1043. Is It a Binary Search Tree (25)
1020. Tree Traversals (25)
Lecture 3:乘法和逆矩阵
Lecture 2:矩阵消元
Lecture 1:方程组的几何解释
单调队列优化版多重背包
滑动窗口(单调队列,模板题)
热门文章
逛画展(双指针)
发射站(单调队列)
拦截导弹(dp、贪心、结论)
2 误差
爬虫1.4-多线程和队列
爬虫1.3-数据存储
爬虫1.2-数据解析
爬虫1.1-基础知识+requests库
论文笔记-用户危险行为预测
javascript30--day03--Css Variables
Copyright © 2011-2022 走看看