递归方法相关问题

一、青蛙跳台阶问题(类似于斐波那契数列)

1、递归写法(代码重复运行,不高效)

public static int frog(int n){
    if(n <= 0){
        return -1;
    }
    if(n == 1|| n == 2){
        return n;
    }else {
        return frog(n-1) + frog(n-2);
    }
}
public static void main(String[] args) {
    System.out.println(frog(3));
}

2、非递归写法(循环写法)

 public static int frogJump(int n){
        if(n <= 0){
            return -1;
        }
        if(n == 1|| n == 2){
            return n;
        }
        int f1 = 1;
        int f2 = 2;
        int f3 = 0;
        for (int i = 3; i <= n ; i++) {//避免重复运行,代码效率高
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }
    public static void main(String[] args) {
        System.out.println(frogJump(3));
    }

二、递归求解汉诺塔问题(不常问)

一个盘子:A—>C (一次)
两个盘子:A—>B A—>C B—>C (三次)
三个盘子:A->C A->B C->B A->C B->A B->C A->C (七次)
N个盘子: (2^N-1次)

public class TestDemo {
    public static void hanio(int n,char pos1,char pos2,char pos3) {
     //pos1起始位置 pos2中转位置 pos3目的位置
        if(n == 1) {
            move(pos1,pos3);
        }else {
            hanio(n - 1,pos1,pos3,pos2);
            move(pos1,pos3);
            hanio(n - 1,pos2,pos1,pos3);
        }
    }
    public static void move(char pos1,char pos2) {
        System.out.print(pos1 + "->" + pos2 + " ");
    }
    public static void main(String[] args) {
        hanio(1,'A','B','C');
        System.out.println();
        hanio(3,'A','B','C');
    }
}

三、求斐波那契数列的第 N 项

1、递归写法

public class TestDemo {
   public static int fib1(int n) {
        if( n == 1 || n == 2) {
            return 1;
        }
        return fib1(n - 1) + fib1(n - 2);//进行了过度冗余计算
    }
    public static void main(String[] args) {
        System.out.println(fib(34));
    }
}

2、非递归写法(循环写法)

public class TestDemo {
   public static int fib(int n) {
        if(n == 1 || n == 2) {
            return 1;
        }
        int f1 = 1;
        int f2 = 1;
        int f3 = 0;
        for (int i = 3; i <= n; i++) {
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }
    public static void main(String[] args) {

        System.out.println(fib(40));
    }
}

四、写一个递归方法,输入一个非负整数,返回组成它的数字之和

public class TestDemo { 
   public static int sum1(int n) {
        if(n > 9) {
            return n % 10 + sum1(n/10);
        }
        return n;
    }
    public static void main(String[] args) {
        System.out.println(sum1(1729));
    }
}

五、递归按顺序打印一个数字的每一位(例如 1234 打印 1 2 3 4)

1、把if条件写为n > 9

public class TestDemo {
   public static void print(int n) {
        if(n > 9) {
            print(n / 10);
        }
        System.out.println(n % 10);
    }
    public static void main(String[] args) {
       print(9568);
    }
}

1、把if条件写为n <= 9

public class TestDemo {
    public static void print1(int n) {
        if(n <= 9) {
            System.out.println(n % 10);
        }else{
            print(n / 10);
            System.out.println(n % 10);
        }
    }
    public static void main(String[] args) {
        print1(9568);
    }
}

六、递归求 1 + 2 + 3 + … + 10的和

public class TestDemo {
   public static int sum(int n) {
        if (n == 1) {
            return 1;
        }else {
        return n + sum(n - 1);
        }
    }
    public static void main(String[] args) {
        System.out.println(sum(10));
    }
}

七、递归求 N 的阶乘

public class TestDemo {
   public static int fac(int n) {
         if(n == 1) {
             return 1;
         }
         int tmp =n *fac(n-1);
         return tmp;
    }
    public static void main2(String[] args) {
        System.out.println(fac(5));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值