递归的学习

递归

汉诺塔

package 算法.递归.汉诺塔;

/**
 * 定义六个不同函数分别表示汉诺塔移动的方向
 */
public class 汉诺塔01版本 {
    static String a = "A塔";
    static String c = "C塔";
    static String b = "B塔";

    public static void main(String[] args) {
        hannuota(3);
    }

    public static void hannuota(int n) {
        leftToRight(n);
    }

    /**
     * 将1——n的圆盘从左移动右
     */
    public static void leftToRight(int n) {
        if (n == 1) {
            //打印第1个圆盘从A塔移动到C塔的过程
            System.out.println("第" + n + "个圆盘从" + a + "移动到" + c);
            return;
        }
        //调用函数将n-1个圆盘从A塔移动到B塔
        leftToMid(n - 1);
        //打印第n个圆盘从A塔移动到C塔的过程
        System.out.println("第" + n + "个圆盘从" + a + "移动到" + c);
        //调用函数将n-1个圆盘从B塔移动到C塔
        midToRight(n - 1);
    }

    /**
     * 将1——n的圆盘从左移动中
     */
    public static void leftToMid(int n) {
        if (n == 1) {
            //打印第1个圆盘从A塔移动到B塔的过程
            System.out.println("第" + n + "个圆盘从" + a + "移动到" + b);
            return;
        }
        //调用函数将n-1个圆盘从A塔移动到C塔
        leftToRight(n - 1);
        //打印第n个圆盘从A塔移动到B塔的过程
        System.out.println("第" + n + "个圆盘从" + a + "移动到" + b);
        //调用函数将n-1个圆盘从C塔移动到B塔
        rightToMid(n - 1);
    }

    /**
     * 将1——n的圆盘从右移动中
     */
    public static void rightToMid(int n) {
        if (n == 1) {
            //打印第1个圆盘从C塔移动到B塔的过程
            System.out.println("第" + n + "个圆盘从" + c + "移动到" + b);
            return;
        }
        //调用函数将n-1个圆盘从C塔移动到A塔
        rightToLeft(n - 1);
        //打印第n个圆盘从C塔移动到B塔的过程
        System.out.println("第" + n + "个圆盘从" + c + "移动到" + b);
        //调用函数将n-1个圆盘从A塔移动到B塔
        leftToMid(n - 1);
    }

    /**
     * 将1——n的圆盘从右移动左
     */
    public static void rightToLeft(int n) {
        if (n == 1) {
            //打印第1个圆盘从C塔移动到A塔的过程
            System.out.println("第" + n + "个圆盘从" + c + "移动到" + a);
            return;
        }
        //调用函数将n-1个圆盘从C塔移动到B塔
        rightToMid(n - 1);
        //打印第n个圆盘从C塔移动到A塔的过程
        System.out.println("第" + n + "个圆盘从" + c + "移动到" + a);
        //调用函数将n-1个圆盘从B塔移动到A塔
        midToLeft(n - 1);
    }

    /**
     * 将1——n的圆盘从中移动左
     */
    public static void midToLeft(int n) {
        if (n == 1) {
            //打印第1个圆盘从B塔移动到A塔的过程
            System.out.println("第" + n + "个圆盘从" + b + "移动到" + a);
            return;
        }
        //调用函数将n-1个圆盘从B塔移动到C塔
        midToRight(n - 1);
        //打印第n个圆盘从B塔移动到A塔的过程
        System.out.println("第" + n + "个圆盘从" + b + "移动到" + a);
        //调用函数将n-1个圆盘从C塔移动到A塔
        rightToLeft(n - 1);
    }

    /**
     * 将1——n的圆盘从中移动右
     */
    public static void midToRight(int n) {
        if (n == 1) {
            //打印第1个圆盘从B塔移动到C塔的过程
            System.out.println("第" + n + "个圆盘从" + b + "移动到" + c);
            return;
        }
        //调用函数将n-1个圆盘从B塔移动到A塔
        midToLeft(n - 1);
        //打印第n个圆盘从B塔移动到C塔的过程
        System.out.println("第" + n + "个圆盘从" + b + "移动到" + c);
        //调用函数将n-1个圆盘从A塔移动到C塔
        leftToRight(n - 1);
    }
}


package 算法.递归.汉诺塔;

/**
 * 汉诺塔01版本定义六个函数比较麻烦,将其进行简化:=》6合1版本
 */
public class 汉诺塔02版本 {


    public static void main(String[] args) {
        String a = "A塔";
        String c = "C塔";
        String b = "B塔";
        hannuota(3, a, b, c);
    }

    /**
     * @param n:圆盘数
     * @param a:圆盘的起始位置
     * @param b:圆盘的中间过渡位置
     * @param c:圆盘最终的目标位置
     */
    public static void hannuota(int n, String a, String b, String c) {
        if (n == 1) {
            //打印第1个圆盘从起始位置移动到最终目标位置的过程
            System.out.println("第" + n + "个圆盘从" + a + "移动到" + c);
            return;
        }
        //将n-1的圆盘从起始位置移动到中间过渡位置的过程
        hannuota(n - 1, a, c, b);
        //打印第n个圆盘从起始位置移动到最终目标位置的过程
        System.out.println("第" + n + "个圆盘从" + a + "移动到" + c);
        //将n-1的圆盘从中间过渡位置移动到最终目标位置的过程
        hannuota(n - 1, b, a, c);
    
}

通过汉诺塔01版本和汉诺塔02版本可以发现:

递归可以通过增加参数的方式得到更多可能性

打印字符串的全部子序列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值