递归C ++函数,它将整数数组n作为输入,然后在不使用任何循环的情况下打印数组中的最大值和最小值

时间:2015-02-23 15:55:48

标签: c++ arrays recursion max min

你好我有一个功课问题我被困在...任何暗示或提示将不胜感激。问题是:

编写一个简短的递归C ++函数,它将整数数组作为输入 size n然后打印数组中的最大值和最小值而不使用 任何循环。

到目前为止,我有以下内容:

/* Precondition: the function is given the array, first Index and last index, takes two int variable maxim, minim set to 0
                 also takes a bool isFirst. Not sure if I really need the bool isFirst to display the min and max values from the
                 function it self.
   Postcondition: the function displays the maximum and the minimum in the array.
*/
void findMaxMin(int A[], int firstIndex, int lastIndex, int &maxim, int &minim, bool isFirst)
{
    if (firstIndex == lastIndex) // Base case one element in the array
    {
        maxim = A[firstIndex];
        minim = A[firstIndex];
        return;
    }
else
{
    int myMin = 0;
    int myMax = 0;

findMaxMin(A, firstIndex, (lastIndex-1), myMin, myMax,  false);

        if(A[lastIndex] < myMin)
            myMin = A[lastIndex];

        if (A[lastIndex] > myMax)
            myMax = A[lastIndex];

            maxim = myMax;
            minim = myMin;

cout<<"mymin now is: "<< myMin<<endl;
cout<< "mymax now is: "<<myMax<<endl;

        //if (isFirst)
        //    cout<< " my min is" << minim << " my max is"<< maxim<< endl;


}
}

然而这不应该工作......不确定问题出在哪里。

分而治之的方法尝试1: 这个问题唯一的问题是它在每次递归调用时打印最小值和最大值,但代码似乎是正确的。我发现的cout问题的解决方案是从main打印,但问题是指定打印是从函数内部进行的;但是找不到从功能打印的方法只有最后的比较;它始终打印在每次递归调用上

void MaxMin(int A[], int firstIndex, int lastIndex, int &maxim, int &minim)
{

    if(firstIndex == lastIndex) // base case one element
    {
        maxim = A[firstIndex];
        minim = A[firstIndex];
        return;
    }
    else if(firstIndex == lastIndex -1) // base case two element
    {
        if(A[firstIndex] < A[lastIndex])
        {
            maxim = A[lastIndex]; 
            minim = A[firstIndex];
        }
        else
        {
            maxim = A[firstIndex]; 
            minim = A[lastIndex];
        }
     }

    else // there are more than two elements in the array
    {
        int mid = (firstIndex+lastIndex)/2;

        MaxMin(A,firstIndex,mid,maxim,minim);

        int maxim1 =0;
        int minim1 =0;

        MaxMin(A,mid+1,lastIndex, maxim1, minim1);

        if(maxim < maxim1)
        {
            maxim = maxim1;
        }

        if(minim > minim1)
        {
            minim = minim1;
        }
    }

//每次递归调用都会打印出来。

cout<<"my minimum now is "<<minim<<endl;
cout<<" my maximum now is "<<maxim<<endl;

}

2 个答案:

答案 0 :(得分:3)

问题在于函数声明int &maximint &minim之前,在递归调用中,您首先放置最小值。所以你需要打电话:

  

findMaxMin(A,firstIndex,(lastIndex-1),myMax,myMin,false);

您构建代码的方式firstIndex也只能为0.因此您可以删除此参数并替换为0.此外,不需要isFirst。如果您直接使用minimmaxim代替myMinmyMax,则可以进一步简化。

答案 1 :(得分:1)

作为提示而非定义解决方案。任何形式的循环

for(int loopIndex, loopContinueCondition(loopIndex), loopIncrement(loopIndex))

可以转换为表单的递归

ResultType my_for(int loopIndex, ResultType partialResult, ...) {
    ... //normal loop body
    newPartialResult = ...;
    if (loopContinueCondition(loopIndex)) {
        loopIncrement(loopIndex);
        return my_for(loopIndex, newPartialResult, ...);
    } else
        return newPartialResult;
}

其中...表示循环体中通常可用的附加数据。