查找最大值的递归方法

时间:2014-10-09 15:36:06

标签: java arrays algorithm recursion max

maxRec()用于使用帮助程序计算数组中的最大值 方法maximize()。但是,当执行此代码时,它似乎总是返回零 它会打印出正确的值。使用调试器时,我注意到了 maxRec()方法将获得正确的返回值,但不会返回它;相反,它将其设置回零并向上移动到else语句。我将不胜感激任何有助于解决此问题的建议。

public int maxRec(int[] v) {
    int maxValue = 0;
    int[] tempArray = maximize(v);
    boolean executeCode = true;
    if (tempArray.length == 1) {
        maxValue = tempArray[0];
        executeCode = false;
        System.out.println(maxValue);
    } else if (executeCode == true && tempArray.length != 1) {
        maxRec(tempArray);
    }
    return maxValue;
}

public int[] maximize(int[] v) {
    int count = 0;
    int secondCount = 0;
    for (int i = 0; i < v.length; i++) {
        if (v[i] > v[0]) {
            count++;
        }
    }
    int[] newArray;
    newArray = new int[count];
    for (int i = 0; i < v.length; i++) {
        if (v[i] > v[0]) {
            newArray[secondCount] = v[i];
            secondCount++;
        }
    }
    return newArray;
}

3 个答案:

答案 0 :(得分:0)

代码应该像这样改变。

public int maxRec(int[] v)
{
    int maxValue=0;
    int[] tempArray = maximize(v);
    boolean executeCode = true;
    if(tempArray.length==1)
    {
        maxValue=tempArray[0];
        executeCode=false;
    }
    else if(executeCode==true && tempArray.length!=1 && tempArray.length > 0)
    {
         maxValue = maxRec(tempArray);
    }
    return maxValue;
}

public int[] maximize(int[] v)
{
    int count=0;
    int secondCount=0;
    for(int i=0;i<v.length;i++)
    {
        if(v[i]>v[0])
        {
            count++;
        }
    }
    int[] newArray;
    newArray = new int[count];

    if(count == 0)
    {
        newArray = new int[1];
        newArray[0] = v[0];
        return newArray;
    }

    for(int i=0;i<v.length;i++)
    {
        if(v[i]>v[0])
        {
            newArray[secondCount]=v[i];
            secondCount++;
        }
    }
    return newArray;
}

答案 1 :(得分:0)

maximize返回一个大于数组第一项的所有值的数组。

要制作递归函数,最简单的情况,最少的工作。 剩下的一个代表自己的克隆,递归调用。

public int maxRec(int[] v) {
    if (v.length == 0) {
        throw IllegalArgumentException();
    }
    int[] greaterThanFirst = maximize(v);

    int maxValue = 0;
    if (greaterThanFirst.length == 0) {
        maxValue = v[0];
    } else {
        maxValue = maxRec(greaterThanFirst);
    }
    return maxValue;
}

首先进行健全性检查,不要空着。 如果最大值没有产生更大的数字,则产生第一个值,即最大值。

答案 2 :(得分:0)

//-------------------------------------------------------------------
// 1. maxRec --> Computes the maximum item of MyList 
//-------------------------------------------------------------------   
/**
 * The function computes the maximum item of m (-1 if m is empty). 
 * @param m: The MyList we want to compute its maximum item.
 * @return: The maximum item of MyList    
 */ 
public int maxRec(MyList<Integer> m){
    int max = 0;
    int res = 0;
    int e0 = 0;
    int e1 = 0;

    // Scenarios Identification
    int scenario = 0;

    // Type 1. MyLyst is empty
    if(m.length() == 0) {
        scenario = 1;
    }else {
        scenario = 2;
    }

    // Scenario Implementation
    switch(scenario) {

    // If MyLyst is empty
    case 1:
        res = -1;
        break;
        // If there is 1 or more elements   
    case 2:
        // Old School
        for(int i = 0; i <= m.length()-1; i++)
            if(m.getElement(i) > max) {
                max = m.getElement(i); 
            }
        // Recursively

        //1. Get and store first element of array
        e0 = m.getElement(0);
        //2. We remove the first element from MyList we just checked
        m.removeElement(0);
        //3. We recursively solve the smaller problem
        e1 = maxRec(m); 
        //4. Compare and store results
        if(e0 > e1) {
            res = e0;
        }
        else {
            res = e1;
        }
        //5. Return removed element back to the array
        m.addElement(0, e0);
        break;
    }
    //6.Display the process to see what's going on
    System.out.println("My Way: "+ max);
    System.out.println("Recursive Way: " + res);
    //7. Return result 
    return res;
}