睡不着,随便写了下汉诺塔的解决方法。

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)
如对本文有什么疑问,请在下面写下留言,谢谢!

posted on 2008-05-30 02:36 张旋 阅读(74) 评论(2)  编辑 收藏 网摘 所属分类: .Net Framework / C# 随便写写技术点滴我的程序

评论

#1楼  2008-10-12 13:41 国赋电子 [未注册用户]

现在在哪里高就,还在国赋电子吗?
email:neu_soft@yeah.net   回复  引用    

#2楼 [楼主] 2008-10-14 13:26 张旋      

感謝關注,現在已經不在國賦電子工作了。   回复  引用  查看    


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
Google站内搜索

相关文章:

相关链接:


 
<2008年5月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

公告

天道是啥?酬勤。

与我联系

搜索

 

常用链接

留言簿(8)

我参与的团队

随笔分类

随笔档案

友情Blog

最新评论

阅读排行榜

评论排行榜