2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
Case 1: 14 1 4 Case 2: 7 1 6
<span style="font-size:24px;">#include<iostream>
using namespace std;
int main()
{
int i,j,k=0,t,n,a,start,end,max,temp;
cin>>t;
for(i=1;i<=t;i++)
{
max=-1001,temp=start=k=0;
scanf("%d",&n);
for(j=0;j<n;j++)
{
scanf("%d",&a);
temp+=a;
if(temp>max)
{
start=k;
end=j;
max=temp;
}
if(temp<0)//再往后加会“拖累”后面的和。
{
temp=0;
k=j+1;
}
}
printf("Case %d:\n",i);
printf("%d %d %d\n",max,start+1,end+1);
if(i!=t)
cout<<endl;
}
return 0;
}</span>杭电 1003 Max Sum,布布扣,bubuko.com
原文:http://blog.csdn.net/u012766950/article/details/38277169