您的位置:首页 > 其它

在规模为n的数据元素集合中找出最大元

2017-09-29 17:06 197 查看
#include <iostream>

#include <algorithm>

#include <cstdio>

#include <string.h>

#include <math.h>

/**

在规模为n的数据元素集合中找出最大元

*/

using namespace std;

int maxElement(int a[], int l, int r) {

    if(l==r) return a[l];

    return max(maxElement(a, l, (l+r)/2), maxElement(a, (l+r)/2+1, r));

}

int main() {

    int a[1024];

    int n;

    cout <<"数列长度:"<<endl;

    scanf("%d",&n);

    if(n == 0){

        puts("数列长度不能为0!!!!");

        return 0;

    }

    cout << "请输入数列:" <<endl;

    for (int i=0;i<n;i++){

        scanf("%d" ,&a[i]);

    }

    printf("这个数列的最大元为: %d\n",maxElement(a,0,n-1));

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐