/*
* 算法——求数组中最大子数组和
*/
#include <iostream>
#include <vector>
using namespace std;
using std::vector;
vector<int> ivec;
int Max_SubArray(int *pia,int len,int *start_script,int *end_script)
{
int max_sum=0;
int i;
int temp_sum=0;
int start,end;
for(i = 0;i != ivec.size();++i)
{
if(temp_sum <= 0)
{
temp_sum=pia[i];
end=i;
start=i;
}
else
{
temp_sum += pia[i];
end=i;
}
if(temp_sum > max_sum)
{
max_sum=temp_sum;
*start_script=start;
*end_script=end;
}
}
if(max_sum==0)
{
max_sum=pia[0];
*start_script=0;
*end_script=0;
for(i = 0;i != ivec.size();++i)
{
if(pia[i] > max_sum)
{
max_sum=pia[i];
*start_script=i;
*end_script=i;
}
}
}
return max_sum;
}
int main()
{
int ival;
int len;
int start,end;
int re;
cin>>ival;
while (ival != 0){//输入容器内元素最后以0结束
ivec.push_back(ival);
cin >> ival;
}
len=ivec.size();
vector<int>::iterator iter = ivec.begin();//定义了一个迭代器类型变量iter,并且初始化指向ivec容器第一个元素
//printf("%d\n",len);
if(ivec.begin()==ivec.end())//vector 为空
cout << "The Vector is Empty!" << endl;
else{
re=Max_SubArray(iter,len,&start,&end); //s,l以引用的形式传入函数,函数处理后返回相应值
cout<<"Max_Sub_Array is " << re <<" And Array_Subscript From " << start << " To "<< end <<endl;
}
return 0;
}