Java____ method recursion problem Tower of Hanoi problem and frog jumping steps problem

method recursion

concept

Method recursion: Program structure, which is the basis in the data structure and algorithm section.
Data Structure: How data is stored.
Algorithms: How data is processed.

in conclusion

1. In what scenarios can recursion be used?
1. A large problem can be divided into several sub-problems;
2. The original problem and the sub-problem have the same solution except for the different data scales;
3. There is a recursive termination condition.
2. What is method recursion?
The process of a method calling itself is called method recursion (remember the semantics of the method!!! Be sure not to think about internal implementation)

Example: Write a recursive function to implement the factorial of 5

public class Recursion {
    
    
    public static void main(String[] args) {
    
    
        int num = 5;
        System.out.println(factor(5));

    }
    //写一个递归函数实现5!
    //一定注意方法的语义(这个方法能干嘛)
    //factor(num) 传入 num -> num!
    //5! 5 * factor (4)
    public static int factor(int num){
    
    
        //先写终止条件
        if (num == 1){
    
    
            return num;
        }
        return num * factor(num - 1);
    }
}

insert image description here

Example: Implementing 1+2+…+10 with recursion

public class Recursion {
    
    
    public static void main(String[] args) {
    
    
        int num = 10;
        System.out.println(add(10));

    }
public static int add(int num){
    
    
        if (num == 1){
    
    
            return 1;
        }
        return num + add(num - 1);
    }
}

insert image description here

Example: Enter a non-negative integer and return the sum of the numbers that make it up

public class Recursion {
    
    
    public static void main(String[] args) {
    
    
        int num = 1729;
        System.out.println(sum(num));

    }
//输入一个非负整数,返回组成他的数字之和
public static int sum(int num){
    
    
        //num只有个数
        //终止条件
        if (num  < 10){
    
    
            return num;
        }
        //num > 10
        //能知道num的个位
        return num % 10 + sum(num / 10);
    }
}

insert image description here

Example: Print each digit of a number in sequence

public class Recursion {
    
    
    public static void main(String[] args) {
    
    
        int num = 1729;
        print(num);


    }
 //按顺序打印数字的每一位
    //传入一个num就能打印每一位的值
    public static void print(int num){
    
    
        if (num > 10){
    
    //递的过程
            //此时num还需要继续寻址最高位
            print(num / 10);
        }
        //此时说明最高位已经找到
        //%10是为了保留其他位
        System.out.print(num % 10 + " ");
    }
}

insert image description here

Example: frog jumping steps

Question: A frog can jump up one step or two steps at a time. How many ways can the frog jump up n steps?

public class Homework2 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(frogJump(5));
    }
    /*
    例1:青蛙跳台阶问题-----斐波那契数的应用
题:一只青蛙一次可以跳上一级台阶,也可以跳上两级,求该青蛙跳上n级台阶一共有多少种跳法?
    */
    public static int frogJump(int n){
    
    
        //先找终止条件  共有一级台阶 n = 1种
        //                二           2种
        if (n == 1){
    
    
            return 1;
        }
        if (n == 2){
    
    
            return 2;
        }
        return frogJump(n-1) + frogJump(n-2);
    }
}

insert image description here

Example: Tower of Hanoi problem

insert image description here

How to achieve finding the largest plate? Put n-1 disks on B. At this time, only the largest disk
A—>C is left on A, and then n-1 disks on B are sequentially ordered from B—>C

public class Homework2 {
    
    
static int count = 0;
    public static void main(String[] args) {
    
    
        int n = 3;
        char A = 'A';
        char B = 'B';
        char C = 'C';
        hanoTower(n,A,B,C);
        System.out.println("一个搬了"+count+"次盘子");
    }

 /*
    例2.汉诺塔问题
       A 起始位置
       C 目标位置
       B 辅助塔 暂存小盘子
    */
    public static void hanoTower(int nDisks,char A,char B,char C){
    
    
        if (nDisks == 1) {
    
    
            //一个盘子,直接从A--->C
            move(1,A,C);
            return;
        }
        //此时有多个盘子
        //核心步骤:先把n-1个盘子从A放到B上
        hanoTower(nDisks - 1, A, C, B);
        //此时找到了最大盘子A-->C
        move(nDisks,A,C);
        //再把B上的盘子依次从B——>C
        hanoTower(nDisks-1, B, A, C);
    }


    /*
    * 将标号为 n 的盘子从sourceTower(原塔)到distTower(目标塔)
    * */
    public static void move(int n,char sourceTower,char distTower){
    
    
    count ++;
        System.out.println("标号为"+n+"盘子从"+sourceTower+"--->"+distTower);

    }
}

insert image description here

Guess you like

Origin blog.csdn.net/biteqq/article/details/122699121