【C++】递归之求数组最大值

前端之家收集整理的这篇文章主要介绍了【C++】递归之求数组最大值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这个比较简单,想清楚基线条件和递归条件就可以了,直接看代码(VS直接运行):

#include<iostream>
#include<vector>
using namespace std;

int arrayMax(int data[],int length);


 main()
{
    int arr[] = {0,5,1)">1,1)">3,1)">9,1)">2,1)">6,1)">7,1)">8,1)">4};
    int length = 10;
     result;
    result = arrayMax(arr,length);
    cout << result << endl;

    
}

 len)
{
    if (len == 1)
        return data[0];
    2)//这一步可以不加,但是加上可以减少一层调用栈的深度
        0] > data[1] ? data[0] : data[else
    {
        int t = data[len - 1];把最后一位当成基准值,比较它和其他位的大小,返回大的
        return t > arrayMax(data,len - 1) ? t : arrayMax(data,1)">);
    }
}

 

猜你在找的算法相关文章