首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >递归和基本流程中的返回

递归和基本流程中的返回
EN

Stack Overflow用户
提问于 2018-06-11 00:37:19
回答 1查看 62关注 0票数 0

我不能理解有或没有return的递归程序的流程,我是返回还是打印函数计算出的一组值?例如,为了计算一个数组中的所有峰值元素,我使用了递归,但我不能得到如何将这些值作为result.Basically给出,我不清楚如果我在递归函数之前编写或不编写return会发生什么。

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-11 01:45:41

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

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

代码语言:javascript
复制
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;
}

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

代码语言:javascript
复制
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;
    }
}

并使用以下命令调用它:

代码语言:javascript
复制
//       array, size of it, initial index, initial sum
peak_sum(arr, array_size, 0, 0);

如果你对你的代码流感兴趣:

代码语言:javascript
复制
//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;
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50785853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档