最大数字子串和
inline void getMaxSub()
{
int a[] = {-3, 4, 9, 2, -10, -7, 11, 3, -8};
int b[] = {-1, 2, 6, -3, 5, -7, 14, -5, -15, 1, 8, -4, 9};
int c[] = {-2, 11, -4, 13, -5, 2, -5, -3, 12, -9};
vector<int> iv(c, c+10);
vector<int>::iterator itmpstart = NULL, istart = NULL, iend = NULL;
vector<int>::iterator it = iv.begin();
int sum = 0, max = 0;
for (; it != iv.end(); it++)
{
if(sum >= 0)
{
sum += *it;
}
else
{
itmpstart = it;
sum = *it;
}
if (sum > max)
{
istart = itmpstart;
max = sum;
iend = it;
}
}
cout << "max sub string is: ";
for (it = istart; it <= iend; it++)
{
cout << *it << " ";
}
cout << "max sum is: " << max << endl;
}
{
int a[] = {-3, 4, 9, 2, -10, -7, 11, 3, -8};
int b[] = {-1, 2, 6, -3, 5, -7, 14, -5, -15, 1, 8, -4, 9};
int c[] = {-2, 11, -4, 13, -5, 2, -5, -3, 12, -9};
vector<int> iv(c, c+10);
vector<int>::iterator itmpstart = NULL, istart = NULL, iend = NULL;
vector<int>::iterator it = iv.begin();
int sum = 0, max = 0;
for (; it != iv.end(); it++)
{
if(sum >= 0)
{
sum += *it;
}
else
{
itmpstart = it;
sum = *it;
}
if (sum > max)
{
istart = itmpstart;
max = sum;
iend = it;
}
}
cout << "max sub string is: ";
for (it = istart; it <= iend; it++)
{
cout << *it << " ";
}
cout << "max sum is: " << max << endl;
}