![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
namespace 汉诺塔
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
class HanoiBuilder
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{void hanoi(int n,string A,string B,string C)
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{if(n==1)
6
Console.WriteLine(A+"------>"+C);
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{ hanoi(n-1,A,C,B);//借助C把A上的盘子移到B
8
Console.WriteLine(A+"------->"+C);
9
hanoi(n-1,B,A,C);//借助A把B上的盘子移到C}}
10
public void Hanoi(int n)
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{hanoi(n,"A","B","C");}
12
}
13
class Test
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{static void Main()
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{HanoiBuilder hb=new HanoiBuilder();
16
hb.Hanoi(2);
17
Console.ReadLine();}
18
}
19
}
posted @
2009-01-18 10:53
小贱
阅读(
155)
评论()
编辑
收藏
举报