标签:求数组最大子数组之和
#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;
}
标签:求数组最大子数组之和
原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/46406505