汉诺塔(CSharp

using System;
using System.Collections.Generic;
using System.Text;
    /*
     *
     *   * 经典递归之―汉诺塔!
     *
     */
    class HanNuoTa
    {
        static void Main(string[] args)
        {
            HanNuoTa t = new HanNuoTa();
            //通过实例化对象调用方法,搬10个塔
            t.Method('A', 'B', 'C', 10);
        }
       /*
  •         from,汉诺塔所在初始位置,
  •         end ,最终汉诺塔放置位置,
  •         other 中转位置。
        */
        void Method(char from, char end, char other, int number)
        {
            if (number == 1)
            {
                Console.WriteLine(from + "  >>  " + end);
            }
            else
            {
   // 1.当number大于1时,先把最上面的number-1个塔从from转移  other, 借助于end !
                Method(from,other,end,number-1);
 
 
 
   // 2.再把from上最上面的一个转移到   end!
 
                Method(from, end, other, 1);
   // 3.然后把other 上面number-1个塔从other转移  end, 借助于from!
                Method(other, end, from, number - 1);
            }
        }
 
    }
分享至
一键收藏,随时查看,分享好友!
0人
了这篇文章
类别:未分类┆阅读( 0)┆评论( 0) ┆ 返回博主首页┆ 返回博客首页
上一篇 货币大写形式的输出 下一篇 使用分治法求数组的最大值

你可能感兴趣的:(算法,职场,休闲)