02java递归

 一、快速入门

//打印问题
package Project0101;
public class Recursion01 {
    public static void main(String[] args) {
        T t1=new T();
        t1.test(4);
    }
}
class T{
    public void test(int n){
        if(n>2){
            test(n-1);
        }
        System.out.println("n="+n);
    }
}

二、 递归调用机制解析

 三、递归的重要规则

四、递归练习题:

1、求斐波那契数:

使用递归的方式求出斐波那契数1,1,2,3,5,8,13……(从第三个数开始,是前两个数的和)给你一个整数,求出它的值

//求斐波那契数
/*我的答案:
package Project0101;
import java.sql.SQLOutput;
public class RecursionExercise01 {
    public static void main(String[] args) {
        TT t2=new TT();
        int sum=0,n=4;
        for(int i=1;i<=n;i++){
            sum+= t2.fibonacci(i);
        }
        System.out.println(sum);
    }
}
class TT {
    public int fibonacci(int n) {
            if (n == 1) {
                return 1;
            } else if (n == 2) {
                return 1;
            } else {
                return fibonacci(n - 1) + fibonacci(n - 2);
            }
        }
}*/
//老师的答案:
package Project0101;
import java.sql.SQLOutput;
public class RecursionExercise01 {
    public static void main(String[] args) {
        //创建一个对象t
        TT t = new TT();
        //想要得到第n个斐波那契数
        int n=1;
        //对象t调用方法
        int res = t.fibonacci(n);
        //输入是否正确
        if (res != -1) {
            System.out.println("n="+n+"对应的斐波那契数=" + res);
        }
    }
}
class TT{
    public int fibonacci(int n){
        //输入正确
        //n=0为递归结束的条件
        if(n>=1){
            if(n==1||n==2){
                return 1;
            }else{
                return fibonacci(n-1)+fibonacci(n-2);
            }
        }else{//输入错误
            System.out.println("要求输入n>=1的整数");
            return -1;//必须要有返回值,否则报错
        }
    }
}

2、猴子吃桃:

有一堆桃子,猴子第一天吃了其中的一半,并再多吃了一个,以后每天猴子都吃其中的一半,然后再多吃一个。当到第10天时,想再吃时(即还没吃),发现只有一个桃子了。问:最初共有多少个桃子?

//猴子吃桃
//我的答案:
/*package Project0101;
public class RecursionExercise02 {
    public static void main(String[] args) {
        CC t3=new CC();
        System.out.println(t3.monkey(8));
    }
}
class CC{
    int sum=0;
    public int monkey(int m) {
        if(m==1)
            return 1;
        else
            return sum=(monkey(m-1)+1)*2;
    }
}*/
/*思路分析:
第9天有4个=(day10+1)*2
第8天有10个=(day9+1)*2
7——22
6——46
5——94
4——190
3——382
2——766
1——1534
规律:前一天的桃子=(后一天的桃子+1)*2
*/
//老师的答案:
package Project0101;
public class RecursionExercise02 {
    public static void main(String[] args) {
        //创建一个对象
        CC t= new CC();
        //求第day天还有peachNum个桃子
        int day=1;
        int peachNum=t.peach(day);
        //输入是否正确
        if(peachNum!=-1){
            System.out.println("第"+day+"天有"+peachNum+"个桃子");
        }
    }
}
class CC{
    public 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("day必须在1-10");
            return -1;
        }
    }
}

3、老鼠出迷宫:

//老鼠出迷宫
//思路:
//1、先创建迷宫,用二维数组表示
//2、规定map数组的元素值:0表示可以走,1表示障碍物
package Project0101;

public class MiGong {
    public static void main(String[] args) {
        int[][] map=new int [8][7];
        //将迷宫的四条边都设置为1
        for(int i=0;i<8;i++) {
            for (int j = 0; j < 7; j++) {
                if (i == 0 || j == 0 || i == 7 || j == 6) {
                    map[i][j] = 1;
                }
            }
        }
        //无规律的障碍处设置
        map[3][1]=1;
        map[3][2]=1;
        //输出当前地图
        System.out.println("=====当前地图情况====");
        for(int i=0;i<map.length;i++) {
            for (int j = 0; j < map[i].length; j++) {
                System.out.print(map[i][j] + " ");//输出一行
            }
            //输出一行后换行
            System.out.println();
        }
        D t1=new D();
        t1.findWay(map,1,1);
        System.out.println("\n====找路的情况如下====");
        for(int i=0;i<map.length;i++) {
            for (int j = 0; j < map[i].length; j++) {
                System.out.print(map[i][j] + " ");//输出一行
            }
            //输出一行后换行
            System.out.println();
        }
    }
}
class D {
    //使用递归回溯思想来解决老鼠出迷宫
    //解读:
    //1、findWay方法用来寻找出迷宫的路径
    //2、找到返回true,否则返回false
    //3、(i,j)为老鼠位置,初始化位置为(1,1)
    //4、0表示可以走;1表示障碍物;2表示可以走;3表示走过,但走不通
    //5、当map[6][5]=2说明找到通路,游戏结束
    //6、老鼠找路策略:下-》右-》上-》左
    public boolean findWay(int[][] map, int i, int j) {//核心代码
        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 {
                return false;
            }
        }
    }

/*程序输出结果:
=====当前地图情况====
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1

====找路的情况如下====
1 1 1 1 1 1 1
1 2 0 0 0 0 1
1 2 2 2 0 0 1
1 1 1 2 0 0 1
1 0 0 2 0 0 1
1 0 0 2 0 0 1
1 0 0 2 2 2 1
1 1 1 1 1 1 1

进程已结束,退出代码0
*/
//修改找路策略,上-》右-》下-》左
    public boolean findWay2(int[][] map, int i, int j) {//核心代码
        if (map[6][5] == 2) {//找到了
            return true;
        } else {
            if (map[i][j] == 0) {//可以走
                map[i][j] = 2;//先假定可以走通
                //使用找路策略
                if (findWay2(map, i - 1, j)) {//先向上走
                    return true;
                } else if (findWay2(map, i, j + 1)) {//向右
                    return true;
                } else if (findWay2(map, i + 1, j)) {//向下
                    return true;
                } else if (findWay2(map, i, j - 1)) {//向左
                    return true;
                } else {//假定错误,路不通
                    map[i][j] = 3;
                    return false;
                }
            } else {
                return false;
            }
        }
    }
/*程序输出结果:
=====当前地图情况====
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1

====修改策略后的找路的情况如下====
1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 0 0 0 0 2 1
1 1 1 0 0 2 1
1 0 0 0 0 2 1
1 0 0 0 0 2 1
1 0 0 0 0 2 1
1 1 1 1 1 1 1

进程已结束,退出代码0
*/
}
//测试回溯(走不通了往回走)——>添加障碍物map[2][2]=1,使用策略1——>结果如下
/*=====当前地图情况====
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 1 0 0 0 1
1 1 1 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1

====找路的情况如下====
1 1 1 1 1 1 1
1 2 2 2 0 0 1
1 3 1 2 0 0 1
1 1 1 2 0 0 1
1 0 0 2 0 0 1
1 0 0 2 0 0 1
1 0 0 2 2 2 1
1 1 1 1 1 1 1

进程已结束,退出代码0
*/


4、汉诺塔:

问题源于印度一个古老的益智玩具。大梵天创造世界时做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。

//汉诺塔:把a塔的所有盘移动到c塔,大盘不能放在小盘上面
package Project0101;
public class HanoiTower {
    public static void main(String[] args) {
        F tower=new F();
        tower.move(2,'a','b','c');
    }
}
class F{
    public void move(int num,char a,char b,char c){//num表示要移动的个数,a,b,c分别表示a塔,b塔,c塔
        //只有一个盘
        if(num==1){
            System.out.println(a+"->"+c);
        }else{
            //有多个盘,可以看成两个,最下面和上面的所有盘(num-1)
            //1、先移动上面所有的盘到b,借助c
            move(num-1,a,c,b);
            //2、把最下面的这个盘,移动到c
            System.out.println(a+"->"+c);
            //3、把b塔的所有盘移动到c,借助a
            move(num-1,b,a,c);
        }
    }
}
/*输出结果:
a->b
a->c
b->c

进程已结束,退出代码0
*/

6、八皇后问题:

在8*8格的国际象棋上摆放八个皇后,使其不能互相攻击,即:任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戏拈秃笔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值