题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1231
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 10010
using namespace std;
int main()
{
int n,i,a[N],s,m,e,k,MAX,sum,st,en;
while(scanf("%d",&n),n)
{
m=k=;
memset(a,,sizeof(a));
for(i=; i<n; i++)
{
scanf("%d",&a[i]);
if(a[i]<)
m++;
if(a[i]==)
k++;
}
if(m==n)
{
printf("0 %d %d\n",a[],a[n-]);
continue;
}
if(m+k==n)//如果a中只有小数和0则输出三个0;
{
printf("0 0 0\n");
continue;
}
sum=s=st=en=e=MAX=;
for(i=; i<n; i++)
{
if(sum+a[i]<a[i])//当a[i]>a[i]+sum时,要从a[i]开始往下加,之前的不要 ;
{
sum=a[i];
s=e=i;
}
else
{
sum+=a[i];
e=i;
if(MAX<sum)
{
MAX=sum;//MAX是最终要输出的数,所以st和en要随之更新;
st=s;
en=e;
} } }
printf("%d %d %d\n",MAX,a[st],a[en]);
}
56 return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define N 11000
int main()
{
int a[N],dp[N]; int n,i,m,MAX,INDEX,index; while(scanf("%d",&n),n)
{
m=;
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]<)
m++;
}
if(m==n)
{
printf("%d %d %d\n",,a[],a[n-]);
continue;
}
memset(dp,,sizeof(dp));
dp[]=MAX=a[];
index=;
for(i=;i<n;i++)
{
dp[i]=max(a[i],a[i]+dp[i-]);
if(MAX<dp[i])
{
MAX=dp[i];
index=i;
}
}
int sum=;
for(i=index;i>=;i--)
{
sum+=a[i];
if(sum==MAX)
{
INDEX=i;
break;
}
}
printf("%d %d %d\n",MAX,a[INDEX],a[index]);
}
return ;
}