【问题标题】:Return in Recursion and basic flow返回递归和基本流程
【发布时间】:2018-11-20 00:04:18
【问题描述】:

我无法理解递归和不返回两者的程序流程。如何返回或打印函数计算的值组?例如,为了计算数组中的所有峰值元素,我使用了递归但我无法得到如何给出这些值作为结果。基本上我不清楚如果我在递归函数之前写或不写 return 会发生什么。

int  peak(int arr[],int i,int size)
{
    while(i<size)    
    {
        if(arr[i]>arr[i+1]&&arr[i]>arr[i-1])
            cout<<arr[i];
        i++;
        return peak(arr,i,size);
    }
}

【问题讨论】:

  • 而且这个函数没有返回最后一个峰值
  • 你的算法没有多大意义。你同时使用循环迭代?
  • 此外,您超出您的数组范围。
  • @Someprogrammerdude 可能你的意思是 recursion
  • @NiVeR 是的,我愿意

标签: c++ recursion return


【解决方案1】:

至少有两种方法可以解决您的问题。

第一个示例使用循环来计算峰值总和:

int peak_sum(int arr[], int size)
{
    int sum = 0;

    // check first element
    if (arr[0] > arr[1]) {
        std::cout << arr[0] << std::endl;
        sum += arr[0];
    }

    // for each middle value in array
    for (int i = 1; i < size - 2; i++) {
        // if current value is peak
        if(arr[i] > arr[i + 1] && arr[i] > arr[i - 1]) {
            std::cout << arr[i] << std::endl;
            // then we add it to total sum of peaks
            sum += arr[i];
        }
    }

    // check last element
    if (arr[size - 1] > arr[size - 2]) {
        std::cout << arr[size - 1] << std::endl;
        sum += arr[size - 1];
    }    

    // after all we return this sum of peaks
    return sum;
}

但如果你想以递归方式解决它,那么它可能看起来像这样:

int
peak_sum(int arr[], int size, int index, int sum)
{
    // if index in bounds
    if (index < size) {
        // if we on the first element
        if (index == 0) {
            // if first element more then next: it is peak
            if (arr[index] > arr[index + 1]) {
                std::cout << arr[index] << std::endl;
                sum += arr[index];
            }

            // if we on the last element
        } else if (index == size - 1) {
            // if first element more then previous: it is peak
            if (arr[index] > arr[index - 1]) {
                std::cout << arr[index] << std::endl;
                sum += arr[index];
            }

            // else we in the middle
        } else {
            // if current element more then previous and next: it is peak
            if (arr[index] > arr[index - 1] && arr[index] > arr[index + 1]) {
                std::cout << arr[index] << std::endl;
                sum += arr[index];
            }
        }

        // anyway we check next value of array
        // by incrimeting current index
        return peak_sum(arr, size, index + 1, sum);
    } else {
        // otherwise we end iterating array
        return sum;
    }
}

然后调用它:

//       array, size of it, initial index, initial sum
peak_sum(arr, array_size, 0, 0);

如果您对代码流感兴趣:

//example for array_size == 3

if (index in bounds) {
    index++;
    if (index in bounds) {
        index++;
        if (index in bounds) {
            index++;
            if (index in bounds) {}
            else return sum;
        }
    }
}

【讨论】:

  • 也看到这个:ideone.com/jbDPwV。但是,我发现您的回答提供了更多选项 +1。
  • 在迭代方式中,这不会考虑在数组索引 0 和大小为 1 处的极端峰值元素。例如在数组 40 10 20 5 8 中;它只会打印 20 但不会40 和 8。
  • 我执行了递归函数。它没有打印单个峰值元素。我希望打印它。它打印的是 0,因为总和不为零。
  • @ANIKSHA,我刚刚更新了我的代码,现在检查一下。此外,如果数组的大小小于三个元素,则可能会导致一些错误或错误。因此可以通过在每个函数的开头添加特殊的长度检查来改进它。
猜你喜欢
  • 2014-12-25
  • 2019-09-12
  • 1970-01-01
  • 2010-10-30
  • 2015-03-26
  • 1970-01-01
  • 2022-01-03
  • 2011-08-01
  • 1970-01-01
相关资源
最近更新 更多