JAVA:基础递归算法大杂烩

递归思想:

从最外部到达最里部,从最里部开始解决问题,直到解决完最外部的问题。


案例1:

求ABCDE的所有排序


public class Main {

    public static void main(String[] args) {
        char[] data = "ABCDE".toCharArray();
        digui(data, 0);
    }

    private static void digui(char[] data, int curData) {

        if (curData == data.length) {
            for (int i = 0; i < data.length; i++)
                System.out.print(data[i]);
            System.out.println();
        }
        for (int i = curData; i < data.length; i++) {
            {
                char temp = data[curData];
                data[curData] = data[i];
                data[i] = temp;
            }

            digui(data, curData + 1);

            {
                char temp = data[curData];
                data[curData] = data[i];
                data[i] = temp;
            }

        }
    }

}

案例2

求出两个字符串共同最大子集的长度


public class Main {

    public static void main(String[] args) {

        int num=digui("acabczdd","abcsf");
        System.out.println(num);
    }

    private static int digui(String s1, String s2) {

        if(s1.length()==0||s2.length()==0) return 0;

        if(s1.charAt(0)==s2.charAt(0))
            return digui(s1.substring(1), s2.substring(1))+1;
        else
            return Math.max(digui(s1.substring(1),s2 ), digui(s1, s2.substring(1)));
    }

}

案例3:

使用递归打印杨辉三角形


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(digui(i, j) + " ");
            }
            System.out.println();
        }

    }

    private static int digui(int m, int n) {
        if (n == 0)
            return 1;
        if (m == n)
            return 1;
        return digui(m - 1, n) + digui(m - 1, n - 1);
    }

}

案例4

现在有3个红球,2个白球,它们有多少种排列方式

public class Main {

    public static void main(String[] args) {

        System.out.println(digui(3,2));
    }

    private static int digui(int m, int n) {
        if(m==0||n==0) return 1;
        return digui(m-1,n)+digui(m,n-1);
    }

}

案例5

给出一个数字,将这个数字的所有和的表现形式展示出来

输出案例:
7=7
7=6+1
7=5+2
7=5+1+1
7=4+3
7=4+2+1
7=4+1+1+1
7=3+3+1
7=3+2+2
7=3+2+1+1
7=3+1+1+1+1
7=2+2+2+1
7=2+2+1+1+1
7=2+1+1+1+1+1
7=1+1+1+1+1+1+1

public class Main {

    public static int num;

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        num = n;
        int[] a = new int[100];
        digui(n, a, 0);
    }

    private static void digui(int n, int[] a, int k) {
        if (n <= 0) {
            System.out.print(num + "=");
            for (int i = 0; i < k; i++) {

                if (i == k - 1) {
                    System.out.print(a[i]);
                } else {
                    System.out.print(a[i] + "+");
                }
            }
            System.out.println();
            return;
        }

        for (int i = n; i > 0; i--) {
            if (k > 0 && i > a[k - 1])
                continue;
            a[k] = i;
            digui(n - i, a, k + 1);
        }
    }

}

你可能感兴趣的:(JAVA,java,递归算法)