C++求最大子數組之和
#include
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
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;
}