用Java解决三个经典的递归问题

1、Ackermann's function is a recursively-defined function where computing a value requires computation of a simpler case of the function. It is formally defined as a function on two values A(m, n), as follows:

A(m, n) = n + 1 if m = 0
  A(m - 1, 1) if m > 0 and n = 0
  A(m - 1, A(m, n - 1)) if m > 0 and n > 0

Write a Java function to compute Ackermann's function given m and nNote: the results get too large to compute with even small values of n and m. Try A(2, 3) = 9 or A(3, 2) = 29

package work;

public class work1 {

    public static void main(String[] args) {
        int a = ackermannRecursive(2, 3);
        System.out.println(a);
        int b = ackermannRecursive(3, 2);
        System.out.println(b);
    }
    public static int ackermannRecursive(int m, int n) {
        //A(m, n) =    n + 1    if m = 0
        if(m == 0) {
            return n + 1;
        }
        //A(m - 1, 1)    if m > 0 and n = 0
        if(m > 0 && n == 0) {
            return ackermannRecursive(m-1,1);
        }
        //A(m - 1, A(m, n - 1))    if m > 0 and n > 0
        if(m > 0 && n > 0) {
            return ackermannRecursive(m-1,ackermannRecursive(m,n-1));
        }
        return 0;
    }
}

2、Write a recursive method that will calculate and return the range of values in an array. The range is defined as the maximum value in the array minus the minimum value. Hint: you will need a different approach than the solution to problem 2, because you need to calculate both the minimum and maximum values, and a method can have only one return value.

package work;

public class work2 {

    public static void main(String[] args) {
        int a[] = {1,2,3,4,5,6,9};
        System.out.println("数组a的范围为:" + findArrayRange(a));

    }
    public static int  findArrayRange(int a[]) {
        int max = findArrayMaxRecursive(a,a.length);
        int min = findArrayMinRecursive(a,a.length);
        int range = max - min;
        return range;
    }
    //递归寻找数组的最大值
    public static int findArrayMaxRecursive(int a[],int n) {
        int max;
        //数组中就一个数,所以返回它本身
        if(n <= 0) {
            return a[0];
        }
        int t = findArrayMaxRecursive(a,n-1);//求前面n-1个数的最大值
        //若t比第n个数大,则返回t
        if(t > a[n-1]) {
            return t;
        }
        else {
            return a[n-1];
        }
    }
    //递归寻找数组的最小值
    public static int findArrayMinRecursive(int a[],int n) {
        int max;
        //数组中就一个数,所以返回它本身
        if(n <= 0) {
            return a[0];
        }
        int t = findArrayMinRecursive(a,n-1);//求前面n-1个数的最小值
        //若t比第n个数小,则返回t
        if(t < a[n-1]) {
            return t;
        }
        else {
            return a[n-1];
        }
    }
}

 3、Write a recursive method that will find whether or not a particular substring occurs inside another (like indexOf except with a boolean return value). For example, searching "water" for "ate" returns true, but searching "water" for "war" returns false. Here's the catch: you can only use the String charAt and length methods . Also, be careful, because if you search "array" for "ra", the answer is true (don't get caught up looking only at the first "r"). If you like you can have one recursive method that calls another recursive method.

package work;

public class work3 {

    public static void main(String[] args) {
        String str = "hello";
        String subStr = "rlo";
        boolean result = findStrRecursive(str,str.length(),0,subStr,subStr.length(),0);
        System.out.println(result);
    }
    /* 参数说明:
     * str:源字符串
     * strLength:源字符串剩余的未匹配的长度
     * strIndex:下一个匹配的位置
     * subStr:需匹配的字符串
     * subStrLength:需匹配的字符串剩余的未匹配的长度
     * subStrIndex:下一个匹配的位置
     */
    public static boolean findStrRecursive(String str,int strLength,int strIndex,String subStr,int subStrLength,int subStrIndex) {
        //源字符串已无可匹配的元素,但匹配的字符型仍未匹配完
        if(strLength == 0 && subStrLength != 0) {
            return false;
        }
        //char subStrArray[] = subStr.toCharArray();
        //如果需要匹配的字符串的剩余未匹配字符串长度只为1,则它们需匹配的首字符相等即为相等
        if(subStrLength == 1 ) {
            if(str.charAt(strIndex) == subStr.charAt(subStrIndex))
                return true;
            else
                return false;
        }
        else {
            //如果匹配的第一个字符相等,则继续匹配
            if(str.charAt(strIndex) == subStr.charAt(subStrIndex)) {
                //如果继续匹配出现错误
                if(!findStrRecursive(str,strLength-1,strIndex+1,subStr,subStrLength-1,subStrIndex+1)) {
                    //从错误点重新从头开始匹配
                    return findStrRecursive(str,strLength-1,strIndex+1,subStr,subStr.length(),0);
                }
                else {
                    return true;
                }
            }
            //否则前移一个看是否匹配
            else {
                return findStrRecursive(str,strLength-1,strIndex+1,subStr,subStrLength,subStrIndex);
            }
        }
        
    }
}

猜你喜欢

转载自www.cnblogs.com/zlingchao/p/9317755.html