码迷,mamicode.com
首页 > 编程语言 > 详细

10-14解决连续数组子数组最大和

时间:2018-10-14 16:18:10      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:xen   ima   col   图片   style   img   use   []   com   

int max(int a,int b)
{
    if(a>b)
    {
       return a;
    }
    else
    {
       return b;
    }
}
int maxsum(int a[], int n)
{
    int i;
    int maxsofar = 0;
    int maxendinghere = 0;
    for (i = 0; i < n; i++)
    {
        maxendinghere = max(maxendinghere + a[i], 0);
        maxsofar = max(maxsofar, maxendinghere);
    }
    return maxsofar;
}
int main()
{
    int n, i=0;
    cout<<"输入元素个数:";
    cin>>n;
    cout<<"输入整数数组:";
    int a[100000]={0};
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    int max=maxsum(a, n)
    cout << "最大连续子数组的和为:" << max << endl;
    system("pause");
}

输入一个整数数组,数组里有正数也有负数。
数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。
求所有子数组的和的最大技术分享图片技术分享图片技术分享图片

 

 

 

 
 

10-14解决连续数组子数组最大和

标签:xen   ima   col   图片   style   img   use   []   com   

原文地址:https://www.cnblogs.com/qss520/p/9786080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!