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

C++求最大子数组之和

时间:2015-06-08 01:04:53      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:求数组最大子数组之和

#include <iostream>
using namespace std;

int getmaxsum(int a[],int n)
{
    int i = 0;
    int j ;
    int k;
    int sum = 0;
    int save = 0;
    for (; i <= n; i++)
    {
        for (j = 0; j < i; j++)
        {

            for (k = j; k < i; k++)

            {
                sum += a[k];
            }

            save = save>sum ?  save : sum;

            sum = 0;

        }
    }
    cout << save << endl;
    return 0;
}


int main()
{
    int a[] = { 1, 3, 33,-5, 44, 3 };
    getmaxsum(a, 6);
    return 0;
}
#include <iostream>
using namespace std;

int Grial(int a[],int n)
{
    int sum = 0;
    int b = 0;
    for (int i = 0; i < n; i++)
    {
        if (b < 0)
            b = a[i];
        else
            b += a[i];
        if (b>sum)
            sum = b;
    }
    return sum;
}
int main()
{
    int a[] = {2,3,4,5,6,7,8,-22,1};
    cout << Grial(a,9);
    return 0;
}

C++求最大子数组之和

标签:求数组最大子数组之和

原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/46406505

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