韩顺平java07(递归)

递归(recursion)

 

 

 

  • 首先明确一个知识点——方法执行完后会返回到调用它的地方。

小例子:

 

 每个栈都会走玩自己的流程在释放,n=2的时候开始释放,然后逐级返回到上一个栈空间——即调用自己的地方,然后开始打印。

如果改成:

 

则只输出一个n=2,以为在开始返回的时候之前栈的n都>2所以就不走else语句了,只输出一次栈顶的n=2。

 

  • 阶乘(Factorial)
public static void main(String[] args) {

        System.out.println(factorial(5));

    }

    public static int factorial(int i){
        if (i==1){
            return 1;
        }else{
            return factorial(i-1)*i;
        }
    }

 

  •  斐波那契数列(Fibonacci)

 

public static void main(String[] args) {

        for (int i = 1; i < 10; i++) {
            System.out.println("第"+i+"个斐波那契数为:" + fibonacci(i) + "\t");
        }
    }
    public static  int fibonacci(int i){
        if (i==1||i==2){
            return  1;
        }else{
            return fibonacci(i-2)+fibonacci(i-1);
        }
    }

 

  • 猴子吃桃
//猴子吃桃问题----猴子每天吃总数一般的桃子再多吃一个,
    // 第十天想吃的时候发现只剩一个,问一开始有多少桃子
    // 第n天=(第n+1天+1)*2  第十天=1 递归九次 
    public static void main(String[] args) {

        System.out.println(peach(11));
    }

    public static int peach(int day){
        
        if (day == 10){
            return 1;
        }else if (day >= 1 && day <= 9){
            return (peach(day +1 )+1)*2;
        }else{
            System.out.println("没桃子了!");
            return -1;
        }

 

  • 迷宫问题
package oop;

public class MiGong {

    public static void main(String[] args) {

        Tool t = new Tool();
        int[][] map = new int[8][7];
        System.out.println("打印地图:");
        for (int i = 0; i < map.length; i++) {//外循环次数为二维数组的行数 即array.length-1次
            for (int j = 0; j < map[i].length; j++) {//内循环次数为一维数组的长度 即array[i].length-1次
                map[i][0] = 1;
                map[i][6] = 1;
                map[0][j] = 1;
                map[7][j] = 1;
            }

        }
        map[3][1] = 1;
        map[4][5] = 1;
        map[5][4] = 1;

        t.sort(map);

        t.findWay(map,1,1);
        System.out.println("路径:");
        t.sort(map);
    }

}

class Tool {


    public boolean findWay(int[][] map, int i, int j) {
        //0表示路,1表示墙,2表示走过的路,3表示走过但走不通的死路
        //起始位置为(1,1),终点为(6,5)
        //策略为下->右->上->左
        if (map[6][5] == 2) {//表示已经到终点了
            return true;
        }else if(map[i][j] == 0){//表示可以走
            //先假定当前位置可以走通
            map[i][j]=2;
            //开始使用策略
            if (findWay(map,i+1,j)){//如果将地图传入,并且先向下走

                return true;

            }else if (findWay(map,i,j+1)){//向右走

                return true;

            }else if (findWay(map,i-1,j)){//向上走

                return true;

            }else if (findWay(map,i,j-1)){//向左走

                return true;

            }else{
                map[i][j]=3; //四个方向都走不通判定为死路
                return false;
            }

        }else{//可能的情况---1(是墙),2(来路先不回去),3(死路)
            return false;
        }

    }

    public void sort(int[][] array) {//遍历方法
        for (int i = 0; i < array.length; i++) {//外循环次数为二维数组的行数 即array.length-1次
            for (int j = 0; j < array[i].length; j++) {//内循环次数为一维数组的长度 即array[i].length-1次
                System.out.print(array[i][j] + "\t");
            }
            System.out.println();
        }
    }

}

 

  • 汉诺塔问题
public class HanoiTower {
    //汉诺塔问题
    public static void main(String[] args) {

        Tower tower = new Tower();
        tower.move(5,'A','B','C');
        System.out.println("需要"+tower.count+"次");
    }
}

class Tower {
    int count = 0;
    public void move(int num,char a,char b, char c){
        if (num == 1){
            count ++;
            System.out.println(a+" - >" + c);//递归最内层
        }else{
            move(num - 1,a,c,b);//先将a上面的num-1个移动到b上面

            System.out.println(a+" - >" + c); //将a上面的一个移动到c
            count ++;
            move(num-1 ,b,a,c);//先将b上面的num-1个移动到c上面


        }
    }
}

 

posted @ 2021-12-08 21:42  紫英626  阅读(52)  评论(0编辑  收藏  举报

紫英