求一组数列的最大字数组,并求出位置,遍历一遍即可。
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
const int numCount=20;
int numArray[numCount]={11,3,-3,2,45,-6,5,-75,75,-1,-21,-77,-8,-2,-72,-14345,-3245,-55,-28,1111134};
int index1=0,index2=0;
void maxSubArry(){
int max =numArray[0];
int tempMax=0;
int start=0;
int end=0;
int fstart=0,fend=0;
bool first=true;
for(int i=0;i<20;++i){
if(numArray[i]+tempMax>0){
if(first){
start=i;
first=false;
}
tempMax+=numArray[i];
if (tempMax>max){
// cout<<"tempMax"<<tempMax<<"i"<<i<<endl;
end=i;
fstart=start;
max=tempMax;
}
}
else{
tempMax=0;
first=true;
}
}
cout<<"max is:"<<max<<" from :"<<fstart<<" to :"<<end;
}
int main()
{
maxSubArry();
return 0;
}