Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31687 Accepted Submission(s): 14214
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX=10001;
int main()
{
int a[MAX];
int n;
while(scanf("%d",&n)!=EOF&&n)
{
int flag; //表示以当前元素为结尾的 "和(cnt)最大" 的子序列起点。
int cnt; //表示以当前元素为结尾的 "和(cnt)最大" 的子序列 的 "和"。
int mmax; //用来更新 包括当前元素在内的 以及之前的 所有子序列的最大和。
int s,e; //s表示所求序列起点,e表示所求序列终点,下面初始化的时候让s=e=a[0],然后逐步更新s,e。
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cnt=0;
mmax=a[0];
s=e=a[0];
flag=a[0];
for(int i=0;i<n;i++)
{
if(cnt<0) //如果cnt<0,以当前元素为起点构成新的序列
{
cnt=a[i]; //
flag=a[i]; //设置起点
}
else
{
cnt+=a[i];
}
if(mmax<cnt) //更新所求最大值
{
mmax=cnt;
s=flag; //设置所求序列起点
e=a[i]; //设置所求序列终点
}
}
if(mmax<0)
{
cout<<0<<" "<<a[0]<<" "<<a[n-1]<<endl;
continue;
}
else
{
cout<<mmax<<" "<<s<<" "<<e<<endl;
}
}
return 0;
}
原文:http://www.cnblogs.com/f-society/p/6720100.html